Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DirectoryInfo.EnumerateDirectories Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Returns an enumerable collection of directory information in the current directory.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

public IEnumerable<DirectoryInfo> EnumerateDirectories()

Return Value

Type: System.Collections.Generic.IEnumerable<DirectoryInfo>

An enumerable collection of directories in the current directory.

Exception Condition

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).


The caller does not have the required permission.

The EnumerateDirectories and GetDirectories methods differ as follows:

  • When you use EnumerateDirectories, you can start enumerating the collection of DirectoryInfo objects before the whole collection is returned.

  • When you use GetDirectories, you must wait for the whole array of DirectoryInfo objects to be returned before you can access the array.

Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

This method pre-populates the values of the following DirectoryInfo properties:

The returned collection is not cached; each call to the GetEnumerator method on the collection will start a new enumeration.

The following example enumerates the subdirectories under the C:\Program Files directory and uses a LINQ query to return the names of all directories that were created before 2009 by checking the value of the CreationTimeUtc property.

If you only need the names of the subdirectories, use the static Directory class for better performance. For an example, see the EnumerateDirectories(String) method.

DirectoryInfo dirPrograms = new DirectoryInfo(@"c:\program files");
DateTime StartOf2009 = new DateTime(2009, 01, 01);

var dirs = from dir in dirPrograms.EnumerateDirectories()
            where dir.CreationTimeUtc < StartOf2009
            select new
                ProgDir = dir,

foreach (var di in dirs)
    Console.WriteLine("{0}", di.ProgDir.Name);

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.0
Available since 4.0
Return to top
© 2018 Microsoft