DirectoryInfo.EnumerateDirectories DirectoryInfo.EnumerateDirectories DirectoryInfo.EnumerateDirectories DirectoryInfo.EnumerateDirectories Method

Definition

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

Overloads

EnumerateDirectories(String, SearchOption) EnumerateDirectories(String, SearchOption) EnumerateDirectories(String, SearchOption)

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

EnumerateDirectories(String, EnumerationOptions) EnumerateDirectories(String, EnumerationOptions) EnumerateDirectories(String, EnumerationOptions)
EnumerateDirectories() EnumerateDirectories() EnumerateDirectories() EnumerateDirectories()

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

EnumerateDirectories(String) EnumerateDirectories(String) EnumerateDirectories(String) EnumerateDirectories(String)

Returns an enumerable collection of directory information that matches a specified search pattern.

EnumerateDirectories(String, SearchOption) EnumerateDirectories(String, SearchOption) EnumerateDirectories(String, SearchOption)

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

public:
 System::Collections::Generic::IEnumerable<System::IO::DirectoryInfo ^> ^ EnumerateDirectories(System::String ^ searchPattern, System::IO::SearchOption searchOption);
public System.Collections.Generic.IEnumerable<System.IO.DirectoryInfo> EnumerateDirectories (string searchPattern, System.IO.SearchOption searchOption);
member this.EnumerateDirectories : string * System.IO.SearchOption -> seq<System.IO.DirectoryInfo>

Parameters

searchPattern
String String String String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

searchOption
SearchOption SearchOption SearchOption SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Returns

An enumerable collection of directories that matches searchPattern and searchOption.

Exceptions

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.

Examples

The following example, starting from a specified directory, uses this method and the EnumerateFiles method to enumerate the files and directories within the start directory and display details of any files over 10 MB in size.

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        DirectoryInfo diTop = new DirectoryInfo(@"d:\");
        try
        {
            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 (UnauthorizedAccessException UnAuthTop)
                {
                    Console.WriteLine("{0}", UnAuthTop.Message);
                }
            }
            
            foreach (var di in diTop.EnumerateDirectories("*"))
            {
                try
                {
                    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 (UnauthorizedAccessException UnAuthFile)
                        {
                            Console.WriteLine("UnAuthFile: {0}", UnAuthFile.Message);
                        }
                    }
                }
                catch (UnauthorizedAccessException UnAuthSubDir)
                {
                    Console.WriteLine("UnAuthSubDir: {0}", UnAuthSubDir.Message);
                }
            }
        }
        catch (DirectoryNotFoundException DirNotFound)
        {
            Console.WriteLine("{0}", DirNotFound.Message);
        }
        catch (UnauthorizedAccessException UnAuthDir)
        {
            Console.WriteLine("UnAuthDir: {0}", UnAuthDir.Message);
        }
        catch (PathTooLongException LongPath)
        {
            Console.WriteLine("{0}", LongPath.Message);
        }
    }
}
Imports System
Imports System.IO

Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim diTop As New DirectoryInfo("d:\")
        Try
            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 UnAuthTop As UnauthorizedAccessException
                    Console.WriteLine("{0}", UnAuthTop.Message)
                End Try
            Next

            For Each di In diTop.EnumerateDirectories("*")
                Try
                    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 UnAuthFile As UnauthorizedAccessException
                            Console.WriteLine("UnAuthFile: {0}", UnAuthFile.Message)
                        End Try
                    Next
                Catch UnAuthSubDir As UnauthorizedAccessException
                    Console.WriteLine("UnAuthSubDir: {0}", UnAuthSubDir.Message)
                End Try
            Next
        Catch DirNotFound As DirectoryNotFoundException
            Console.WriteLine("{0}", DirNotFound.Message)
        Catch UnAuthDir As UnauthorizedAccessException
            Console.WriteLine("UnAuthDir: {0}", UnAuthDir.Message)
        Catch LongPath As PathTooLongException
            Console.WriteLine("{0}", LongPath.Message)
        End Try
    End Sub
End Class

Remarks

searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern.

Wildcard specifier Matches
* (asterisk) Zero or more characters in that position.
? (question mark) Zero or one character in that position.

Characters other than the wildcard are literal characters. For example, the string "*t" searches for all names in ending with the letter "t". ". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The EnumerateDirectories and GetDirectories methods differ as follows:

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.

EnumerateDirectories(String, EnumerationOptions) EnumerateDirectories(String, EnumerationOptions) EnumerateDirectories(String, EnumerationOptions)

public:
 System::Collections::Generic::IEnumerable<System::IO::DirectoryInfo ^> ^ EnumerateDirectories(System::String ^ searchPattern, System::IO::EnumerationOptions ^ enumerationOptions);
public System.Collections.Generic.IEnumerable<System.IO.DirectoryInfo> EnumerateDirectories (string searchPattern, System.IO.EnumerationOptions enumerationOptions);
member this.EnumerateDirectories : string * System.IO.EnumerationOptions -> seq<System.IO.DirectoryInfo>

Parameters

searchPattern
String String String String

Returns

EnumerateDirectories() EnumerateDirectories() EnumerateDirectories() EnumerateDirectories()

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

public:
 System::Collections::Generic::IEnumerable<System::IO::DirectoryInfo ^> ^ EnumerateDirectories();
public System.Collections.Generic.IEnumerable<System.IO.DirectoryInfo> EnumerateDirectories ();
member this.EnumerateDirectories : unit -> seq<System.IO.DirectoryInfo>
Public Function EnumerateDirectories () As IEnumerable(Of DirectoryInfo)

Returns

An enumerable collection of directories in the current directory.

Exceptions

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.

Examples

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);
}
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

Remarks

The EnumerateDirectories and GetDirectories methods differ as follows:

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.

EnumerateDirectories(String) EnumerateDirectories(String) EnumerateDirectories(String) EnumerateDirectories(String)

Returns an enumerable collection of directory information that matches a specified search pattern.

public:
 System::Collections::Generic::IEnumerable<System::IO::DirectoryInfo ^> ^ EnumerateDirectories(System::String ^ searchPattern);
public System.Collections.Generic.IEnumerable<System.IO.DirectoryInfo> EnumerateDirectories (string searchPattern);
member this.EnumerateDirectories : string -> seq<System.IO.DirectoryInfo>
Public Function EnumerateDirectories (searchPattern As String) As IEnumerable(Of DirectoryInfo)

Parameters

searchPattern
String String String String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.

Returns

An enumerable collection of directories that matches searchPattern.

Exceptions

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.

Remarks

searchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern.

Wildcard specifier Matches
* (asterisk) Zero or more characters in that position.
? (question mark) Zero or one character in that position.

Characters other than the wildcard are literal characters. For example, the string "*t" searches for all names in ending with the letter "t". ". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The EnumerateDirectories and GetDirectories methods differ as follows:

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.

Applies to