Export (0) Print
Expand All

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.EnumerateDirectories

Directory information (DirectoryInfo).

DirectoryInfo.EnumerateDirectories

Files

File names.

Directory.EnumerateFiles

File information (FileInfo).

DirectoryInfo.EnumerateFiles

File system information

File system entries.

Directory.EnumerateFileSystemEntries

File system information (FileSystemInfo).

DirectoryInfo.EnumerateFileSystemInfos

Lines from a text file

Lines from a file.

File.ReadLines

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.

    
    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.

    
    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.
    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.

    
    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

  1. Create a custom method (or function in Visual Basic) to contain your enumeration code.

  2. Apply the MethodImplAttribute attribute with the NoInlining option to the new method. For example:

    [MethodImplAttribute(MethodImplOptions.NoInlining)]
    Private void Enumerate()
    
  3. Include the following method calls, to run after your enumeration code:

Community Additions

ADD
Show:
© 2014 Microsoft