GetDirectories Method (String)

DirectoryInfo.GetDirectories Method (String)

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

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

public DirectoryInfo[] GetDirectories(
	string searchPattern


Type: System.String
The search string. For example, "System*" can be used to search for all directories that begin with the word "System".

Return Value

Type: System.IO.DirectoryInfo[]
An array of type DirectoryInfo matching searchPattern.


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.

Wildcards are permitted. For example, the searchPattern string "*t" searches for all directory names in path ending with the letter "t". The searchPattern string "s*" searches for all directory names in path beginning with the letter "s".

The string ".." can only be used in searchPattern if it is specified as a part of a valid directory name, such as in the directory name "a..b". It cannot be used to move up the directory hierarchy.

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:

The following example counts the directories in a path that contain the specified letter.

using System;
using System.IO;

class Test 
    public static void Main() 
            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, 
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2015 Microsoft