How to: Enumerate Directories and Files
Starting with the .NET Framework version 4, you can enumerate directories and files by using methods that return an enumerable collection of strings of their names. You can also use methods that return an enumerable collection of DirectoryInfo, FileInfo, or FileSystemInfo objects. In previous versions of the .NET Framework, you could only obtain arrays of these collections. Enumerable collections provide better performance than arrays.
You can also use enumerable collections obtained from these methods to supply the IEnumerable<T> parameter for constructors of collection classes such as the List<T> class.
If you want to obtain only the names of directories or files, use the enumeration methods of the Directory class. If you want to obtain other properties of directories or files, use the DirectoryInfo and FileSystemInfo classes. You can also enumerate the lines of a text file.
The following table provides a guide to the methods that return enumerable collections.
To enumerate |
Enumerable collection to return |
Method to use |
---|---|---|
Directories |
Directory names. |
|
Directory information (DirectoryInfo). |
||
Files |
File names. |
|
File information (FileInfo). |
||
File system information |
File system entries. |
|
File system information (FileSystemInfo). |
||
Lines from a text file |
Lines from a file. |
Although you can immediately enumerate all the files in the subdirectories of a parent directory by using the AllDirectories option, unauthorized access exceptions (UnauthorizedAccessException) may cause the enumeration to be incomplete. If these exceptions are possible, you can catch them and continue by first enumerating directories and then enumerating files.
If you are running Windows XP or earlier, a delete operation on a file or directory that follows an enumeration could fail if there is an open handle that remains on one of the enumerated directories or files. If this is occurring, you must induce a garbage collection to remove the open handles.
To enumerate directory names
Use the Directory.EnumerateDirectories(String) method to obtain a list of the top-level directory names in a specified path.
Imports System.Collections.Generic Imports System.IO Imports System.Linq Module Module1 Sub Main() Try Dim dirPath As String = "\\archives\2009\reports" ' LINQ query. Dim dirs = From folder In _ Directory.EnumerateDirectories(dirPath) For Each folder In dirs ' Remove path information from string. Console.WriteLine("{0}", _ folder.Substring(folder.LastIndexOf("\") + 1)) Next Console.WriteLine("{0} directories found.", _ dirs.Count.ToString()) ' Optionally create a List collection. Dim workDirs As List(Of String) = New List(Of String)(dirs) Catch UAEx As UnauthorizedAccessException Console.WriteLine(UAEx.Message) Catch PathEx As PathTooLongException Console.WriteLine(PathEx.Message) End Try End Sub End Module
using System; using System.Collections.Generic; using System.IO; using System.Linq; class Program { private static void Main(string[] args) { try { string dirPath = @"\\archives\2009\reports"; // LINQ query. var dirs = from dir in Directory.EnumerateDirectories(dirPath) select dir; // Show results. foreach (var dir in dirs) { // Remove path information from string. Console.WriteLine("{0}", dir.Substring(dir.LastIndexOf("\\") + 1)); } Console.WriteLine("{0} directories found.", dirs.Count<string>().ToString()); // Optionally create a List collection. List<string> workDirs = new List<string>(dirs); } catch (UnauthorizedAccessException UAEx) { Console.WriteLine(UAEx.Message); } catch (PathTooLongException PathEx) { Console.WriteLine(PathEx.Message); } } }
To enumerate file names in all directories
Use the Directory.EnumerateFiles(String, String, SearchOption) method to search all directories to obtain a list of file names in a specified path that match a specified search pattern.
Imports System.IO Imports System.Xml.Linq Module Module1 Sub Main() Try Dim files = From chkFile In Directory.EnumerateFiles("c:\", "*.txt", _ SearchOption.AllDirectories) From line In File.ReadLines(chkFile) Where line.Contains("Microsoft") Select New With {.curFile = chkFile, .curLine = line} For Each f In files Console.WriteLine("{0}\t{1}", f.curFile, f.curLine) Next Console.WriteLine("{0} files found.", _ files.Count.ToString()) Catch UAEx As UnauthorizedAccessException Console.WriteLine(UAEx.Message) Catch PathEx As PathTooLongException Console.WriteLine(PathEx.Message) End Try End Sub End Module
using System; using System.IO; using System.Linq; class Program { static void Main(string[] args) { try { var files = from file in Directory.EnumerateFiles(@"c:\", "*.txt", SearchOption.AllDirectories) from line in File.ReadLines(file) where line.Contains("Microsoft") select new { File = file, Line = line }; foreach (var f in files) { Console.WriteLine("{0}\t{1}", f.File, f.Line); } Console.WriteLine("{0} files found.", files.Count().ToString()); } catch (UnauthorizedAccessException UAEx) { Console.WriteLine(UAEx.Message); } catch (PathTooLongException PathEx) { Console.WriteLine(PathEx.Message); } } }
To enumerate a collection of DirectoryInfo objects
Use the DirectoryInfo.EnumerateDirectories method to obtain a collection of top-level directories.
' Create a DirectoryInfo of the Program Files directory. Dim dirPrograms As New DirectoryInfo("c:\program files") Dim StartOf2009 As New DateTime(2009, 1, 1) ' LINQ query for all directories created before 2009. Dim dirs = From dir In dirPrograms.EnumerateDirectories() Where dir.CreationTimeUtc < StartOf2009 ' Show results. For Each di As DirectoryInfo In dirs Console.WriteLine("{0}", di.Name) Next
// Create a DirectoryInfo of the Program Files directory. DirectoryInfo dirPrograms = new DirectoryInfo(@"c:\program files"); DateTime StartOf2009 = new DateTime(2009, 01, 01); // LINQ query for all directories created before 2009. var dirs = from dir in dirPrograms.EnumerateDirectories() where dir.CreationTimeUtc < StartOf2009 select new { ProgDir = dir, }; // Show results. foreach (var di in dirs) { Console.WriteLine("{0}", di.ProgDir.Name); }
To enumerate a collection of FileInfo objects in all directories
Use the DirectoryInfo.EnumerateFiles method to obtain a collection of files that match a specified search pattern in all directories. This example first enumerates the top-level directories to catch possible unauthorized access exceptions, and then enumerates the files.
Imports System Imports System.IO Class Program Public Shared Sub Main(ByVal args As String()) ' Create a DirectoryInfo object of the starting directory. Dim diTop As New DirectoryInfo("d:\") Try ' Enumerate the files just in the top directory. For Each fi In diTop.EnumerateFiles() Try ' Display each file over 10 MB; If fi.Length > 10000000 Then Console.WriteLine("{0}" & vbTab & vbTab & "{1}", fi.FullName, fi.Length.ToString("N0")) End If ' Catch unauthorized access to a file. Catch UnAuthTop As UnauthorizedAccessException Console.WriteLine("{0}", UnAuthTop.Message) End Try Next ' Enumerate all subdirectories. For Each di In diTop.EnumerateDirectories("*") Try ' Enumerate each file in each subdirectory. For Each fi In di.EnumerateFiles("*", SearchOption.AllDirectories) Try ' // Display each file over 10 MB; If fi.Length > 10000000 Then Console.WriteLine("{0}" & vbTab & vbTab & "{1}", fi.FullName, fi.Length.ToString("N0")) End If ' Catch unauthorized access to a file. Catch UnAuthFile As UnauthorizedAccessException Console.WriteLine("UnAuthFile: {0}", UnAuthFile.Message) End Try Next ' Catch unauthorized access to a subdirectory. Catch UnAuthSubDir As UnauthorizedAccessException Console.WriteLine("UnAuthSubDir: {0}", UnAuthSubDir.Message) End Try Next ' Catch error in directory path. Catch DirNotFound As DirectoryNotFoundException Console.WriteLine("{0}", DirNotFound.Message) ' Catch unauthorized access to a first tier directory. Catch UnAuthDir As UnauthorizedAccessException Console.WriteLine("UnAuthDir: {0}", UnAuthDir.Message) ' Catch paths that are too long. Catch LongPath As PathTooLongException Console.WriteLine("{0}", LongPath.Message) End Try End Sub End Class
using System; using System.IO; class Program { static void Main(string[] args) { // Create a DirectoryInfo object of the starting directory. DirectoryInfo diTop = new DirectoryInfo(@"d:\"); try { // Enumerate the files just in the top directory. foreach (var fi in diTop.EnumerateFiles()) { try { // Display each file over 10 MB; if (fi.Length > 10000000) { Console.WriteLine("{0}\t\t{1}", fi.FullName, fi.Length.ToString("N0")); } } // Catch unauthorized access to a file. catch (UnauthorizedAccessException UnAuthTop) { Console.WriteLine("{0}", UnAuthTop.Message); } } // Enumerate all subdirectories. foreach (var di in diTop.EnumerateDirectories("*")) { try { // Enumerate each file in each subdirectory. foreach (var fi in di.EnumerateFiles("*", SearchOption.AllDirectories)) { try { // Display each file over 10 MB; if (fi.Length > 10000000) { Console.WriteLine("{0}\t\t{1}", fi.FullName, fi.Length.ToString("N0")); } } // Catch unauthorized access to a file. catch (UnauthorizedAccessException UnAuthFile) { Console.WriteLine("UnAuthFile: {0}", UnAuthFile.Message); } } } // Catch unauthorized access to a subdirectory. catch (UnauthorizedAccessException UnAuthSubDir) { Console.WriteLine("UnAuthSubDir: {0}", UnAuthSubDir.Message); } } } // Catch error in directory path. catch (DirectoryNotFoundException DirNotFound) { Console.WriteLine("{0}", DirNotFound.Message); } // Catch unauthorized access to a first tier directory. catch (UnauthorizedAccessException UnAuthDir) { Console.WriteLine("UnAuthDir: {0}", UnAuthDir.Message); } // Catch paths that are too long. catch (PathTooLongException LongPath) { Console.WriteLine("{0}", LongPath.Message); } } }
To remove open handles on enumerated directories or files
Create a custom method (or function in Visual Basic) to contain your enumeration code.
Apply the MethodImplAttribute attribute with the NoInlining option to the new method. For example:
[MethodImplAttribute(MethodImplOptions.NoInlining)] Private void Enumerate()
Include the following method calls, to run after your enumeration code:
The GC.Collect() method (no parameters).
The GC.WaitForPendingFinalizers() method.