Directory.GetDirectories Method

Definition

Returns the names of subdirectories that meet specified criteria.

Overloads

GetDirectories(String, String, SearchOption)

Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.

GetDirectories(String, String, EnumerationOptions)

Returns the names of subdirectories (including their paths) that match the specified search pattern and enumeration options in the specified directory.

GetDirectories(String, String)

Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

GetDirectories(String)

Returns the names of subdirectories (including their paths) in the specified directory.

GetDirectories(String, String, SearchOption)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.

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

Parameters

path
String

The relative or absolute path to the directory to search. This string is not case-sensitive.

searchPattern
String

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

searchOption
SearchOption

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

Returns

String[]

An array of the full names (including paths) of the subdirectories that match the specified criteria, or an empty array if no directories are found.

Exceptions

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

-or-

searchPattern does not contain a valid pattern.

path or searchPattern is null.

searchOption is not a valid SearchOption value.

The caller does not have the required permission.

The specified path, file name, or both exceed the system-defined maximum length.

path is a file name.

-or-

File or directory is corrupted and unreadable (example: invalid first allocation unit of a FAT32 partition).

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example counts the number of directories that begin with the specified letter in a path. Only the top-level directory is searched.

C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            string[] dirs = Directory.GetDirectories(@"c:\", "p*", SearchOption.TopDirectoryOnly);
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs)
            {
                Console.WriteLine(dir);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Remarks

The path parameter can specify relative or absolute path information, and is not case-sensitive. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

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) Exactly one character in that position.

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

searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.

The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 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, 10
.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

GetDirectories(String, String, EnumerationOptions)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Returns the names of subdirectories (including their paths) that match the specified search pattern and enumeration options in the specified directory.

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

Parameters

path
String

The relative or absolute path to the directory to search. This string is not case-sensitive.

searchPattern
String

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

enumerationOptions
EnumerationOptions

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

Returns

String[]

An array of the full names (including paths) of the subdirectories that match the search pattern and enumeration options in the specified directory, or an empty array if no directories are found.

Exceptions

The caller does not have the required permission.

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using GetInvalidPathChars().

-or-

searchPattern doesn't contain a valid pattern.

path or searchPattern is null.

The specified path, file name, or both exceed the system-defined maximum length.

path is a file name.

-or-

File or directory is corrupted and unreadable (example: invalid first allocation unit of a FAT32 partition).

The specified path is invalid (for example, it is on an unmapped drive).

Remarks

This method returns all subdirectories directly under the specified directory that match the specified search pattern. If the specified directory has no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array. Only the top directory is searched. If you want to search the subdirectories as well, use the GetDirectories(String, String, SearchOption) method and specify AllDirectories in the searchOption parameter.

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) Exactly one character in that position.

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

searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.

The path parameter can specify relative or absolute path information, and is not case-sensitive. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

For a list of common I/O tasks, see Common I/O Tasks.

Applies to

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

GetDirectories(String, String)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.

C#
public static string[] GetDirectories(string path, string searchPattern);

Parameters

path
String

The relative or absolute path to the directory to search. This string is not case-sensitive.

searchPattern
String

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

Returns

String[]

An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.

Exceptions

The caller does not have the required permission.

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using GetInvalidPathChars().

-or-

searchPattern doesn't contain a valid pattern.

path or searchPattern is null.

The specified path, file name, or both exceed the system-defined maximum length.

path is a file name.

-or-

File or directory is corrupted and unreadable (example: invalid first allocation unit of a FAT32 partition).

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example counts the number of directories in a path that begin with the specified letter.

C#
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        try
        {
            // Only get subdirectories that begin with the letter "p."
            string[] dirs = Directory.GetDirectories(@"c:\", "p*");
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs)
            {
                Console.WriteLine(dir);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Remarks

This method returns all subdirectories directly under the specified directory that match the specified search pattern. If the specified directory has no subdirectories, or no subdirectories match the searchPattern parameter, this method returns an empty array. Only the top directory is searched. If you want to search the subdirectories as well, use the GetDirectories(String, String, SearchOption) method and specify AllDirectories in the searchOption parameter.

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) Exactly one character in that position.

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

searchPattern cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.

The path parameter can specify relative or absolute path information, and is not case-sensitive. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 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, 10
.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:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

Returns the names of subdirectories (including their paths) in the specified directory.

C#
public static string[] GetDirectories(string path);

Parameters

path
String

The relative or absolute path to the directory to search. This string is not case-sensitive.

Returns

String[]

An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.

Exceptions

The caller does not have the required permission.

.NET Framework and .NET Core versions older than 2.1: path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars() method.

path is null.

The specified path, file name, or both exceed the system-defined maximum length.

path is a file name.

-or-

File or directory is corrupted and unreadable (example: invalid first allocation unit of a FAT32 partition).

The specified path is invalid (for example, it is on an unmapped drive).

Examples

The following example takes an array of file or directory names on the command line, determines what kind of name it is, and processes it appropriately.

C#
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor
{
    public static void Main(string[] args)
    {
        foreach(string path in args)
        {
            if(File.Exists(path))
            {
                // This path is a file
                ProcessFile(path);
            }
            else if(Directory.Exists(path))
            {
                // This path is a directory
                ProcessDirectory(path);
            }
            else
            {
                Console.WriteLine("{0} is not a valid file or directory.", path);
            }
        }
    }

    // Process all files in the directory passed in, recurse on any directories
    // that are found, and process the files they contain.
    public static void ProcessDirectory(string targetDirectory)
    {
        // Process the list of files found in the directory.
        string [] fileEntries = Directory.GetFiles(targetDirectory);
        foreach(string fileName in fileEntries)
            ProcessFile(fileName);

        // Recurse into subdirectories of this directory.
        string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach(string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);
    }

    // Insert logic for processing found files here.
    public static void ProcessFile(string path)
    {
        Console.WriteLine("Processed file '{0}'.", path);	
    }
}

Remarks

This method is identical to GetDirectories(String, String) with the asterisk (*) specified as the search pattern, so it returns all subdirectories. If you need to search subdirectories, use the GetDirectories(String, String, SearchOption) method, which enables you to specify a search of subdirectories with the searchOption parameter.

The EnumerateDirectories and GetDirectories methods differ as follows: When you use EnumerateDirectories, you can start enumerating the collection of names before the whole collection is returned; when you use GetDirectories, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateDirectories can be more efficient.

The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The names returned by this method are prefixed with the directory information provided in path.

The case-sensitivity of the path parameter corresponds to that of the file system on which the code is running. For example, it's case-insensitive on NTFS (the default Windows file system) and case-sensitive on Linux file systems.

For a list of common I/O tasks, see Common I/O Tasks.

See also

Applies to

.NET 10 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, 10
.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