DirectoryInfo.GetDirectories Method

Definition

Returns the subdirectories of the current directory.

Overloads

GetDirectories()

Returns the subdirectories of the current directory.

GetDirectories(String)

Returns an array of directories in the current DirectoryInfo matching the given search criteria.

GetDirectories(String, EnumerationOptions)

Returns an array of directories in the current DirectoryInfo matching the specified search pattern and enumeration options.

GetDirectories(String, SearchOption)

Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

GetDirectories()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns the subdirectories of the current directory.

C#
public System.IO.DirectoryInfo[] GetDirectories();

Returns

An array of DirectoryInfo objects.

Exceptions

The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive.

The caller does not have the required permission.

The caller does not have the required permission.

Examples

The following example retrieves all the directories in the root directory and displays the directory names.

C#
using System;
using System.IO;

public class GetDirectoriesTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories.
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}

Remarks

If there are no subdirectories, this method returns an empty array. This method is not recursive.

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetDirectories(String)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an array of directories in the current DirectoryInfo matching the given search criteria.

C#
public System.IO.DirectoryInfo[] GetDirectories(string searchPattern);

Parameters

searchPattern
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 array of type DirectoryInfo matching searchPattern.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

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 counts the directories in a path that contain the specified letter.

C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\");

            // Get only subdirectories that contain the letter "p."
            DirectoryInfo[] dirs = di.GetDirectories("*p*");
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length);

            foreach (DirectoryInfo diNext in dirs)
            {
                Console.WriteLine("The number of files in {0} is {1}", diNext,
                    diNext.GetFiles().Length);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

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

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

GetDirectories(String, EnumerationOptions)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an array of directories in the current DirectoryInfo matching the specified search pattern and enumeration options.

C#
public System.IO.DirectoryInfo[] GetDirectories(string searchPattern, System.IO.EnumerationOptions enumerationOptions);

Parameters

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

enumerationOptions
EnumerationOptions

An object that describes the search and enumeration configuration to use.

Returns

An array of type DirectoryInfo matching searchPattern and enumerationOptions.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

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

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

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

GetDirectories(String, SearchOption)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

C#
public System.IO.DirectoryInfo[] GetDirectories(string searchPattern, System.IO.SearchOption searchOption);

Parameters

searchPattern
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

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

Returns

An array of type DirectoryInfo matching searchPattern.

Exceptions

.NET Framework and .NET Core versions older than 2.1: searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

searchOption is not a valid SearchOption value.

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 lists all of the directories and files that begin with the letter "c" in "c:\".

C#
using System;
using System.IO;

class App
{
    public static void Main()
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\";
        string searchPattern = "c*";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] directories =
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);

        FileInfo[] files =
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

        Console.WriteLine(
            "Directories that begin with the letter \"c\" in {0}", path);
        foreach (DirectoryInfo dir in directories)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime);
        }

        Console.WriteLine();
        Console.WriteLine(
            "Files that begin with the letter \"c\" in {0}", path);
        foreach (FileInfo file in files)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", file.Name, file.LastWriteTime);
        }
    } // Main()
} // App()

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

If there are no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array.

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

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0