You can get list of all files in particular directory or folder and it's all sub directories.
.NET Framework 4 allow to enumerate directories and files using methods that returns enumerable collections. There is a also a methods of get enumerable collections for DirectoryInfo , FileSystemInfo and FileInfo objects.
Earlier version of .Net framework provide only array of these collections.
Performance wise enumerable collection is better than arrays.
We are using .NET Framework 4 feature in this example.
We are Using "EnumerateFiles" Method of "Directory" class for get all files in directory.
We are using LINQ query to get list.
For this you have to use System.Collections.Generic and System.IO namespaces.
Here is example for this.
In this example we get list of files name from given directory.
In output you can see that there is a sub folder "New Folder" in "Imp" folder. You can also get this sub folder's file in list.
C# Example :
VB.net Example :
Output :
This type of C# Tips is very useful in day to day programming life.
Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.
.NET Framework 4 allow to enumerate directories and files using methods that returns enumerable collections. There is a also a methods of get enumerable collections for DirectoryInfo , FileSystemInfo and FileInfo objects.
Earlier version of .Net framework provide only array of these collections.
Performance wise enumerable collection is better than arrays.
We are using .NET Framework 4 feature in this example.
We are Using "EnumerateFiles" Method of "Directory" class for get all files in directory.
We are using LINQ query to get list.
For this you have to use System.Collections.Generic and System.IO namespaces.
Here is example for this.
In this example we get list of files name from given directory.
In output you can see that there is a sub folder "New Folder" in "Imp" folder. You can also get this sub folder's file in list.
C# Example :
var lstFiles = from tmpFile in Directory.EnumerateFiles(@"D:\Imp\","*.*", SearchOption.AllDirectories) select new { File = tmpFile }; Response.Write("<br/><b>File Paths:</b>"); foreach (var objFile in lstFiles) { Response.Write(string.Format( "<br/>{0}", objFile.File)); } Response.Write(string.Format("<br/><br/><b>Total {0} files found.</b>", lstFiles.Count().ToString()));
VB.net Example :
Dim lstFiles = From tmpFile In Directory.EnumerateFiles("D:\Imp\", "*.*", SearchOption.AllDirectories) Select New With { _ Key .File = tmpFile _ } Response.Write("<br/><b>File Paths:</b>") For Each objFile In lstFiles Response.Write(String.Format("<br/>{0}", objFile.File)) Next Response.Write(String.Format("<br/><br/><b>Total {0} files found.</b>", lstFiles.Count().ToString()))
Output :
This type of C# Tips is very useful in day to day programming life.
Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.
No comments:
Post a Comment