DirectoryInfo.EnumerateDirectories Method ()

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com 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 Function EnumerateDirectories As IEnumerable(Of DirectoryInfo)

Return Value

Type: System.Collections.Generic.IEnumerable(Of DirectoryInfo)

An enumerable collection of directories in the current directory.

Exception Condition
DirectoryNotFoundException

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

SecurityException

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.

Dim dirPrograms As New DirectoryInfo("c:\program files")
Dim StartOf2009 As New DateTime(2009, 1, 1)

Dim dirs = From dir In dirPrograms.EnumerateDirectories()
            Where dir.CreationTimeUtc < StartOf2009

For Each di As DirectoryInfo In dirs
    Console.WriteLine("{0}", di.Name)
Next

Universal Windows Platform
Available since 10
.NET Framework
Available since 4.0
Silverlight
Available since 4.0
Return to top
Show: