DirectoryInfo.GetDirectories Method (String)
Updated: May 2010
Returns an array of directories in the current DirectoryInfo matching the given search criteria.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- searchPattern
- Type: System.String
The search string. For example, "System*" can be used to search for all directories that begin with the word "System".
| Exception | Condition |
|---|---|
| ArgumentException |
searchPattern contains one or more invalid characters defined by the GetInvalidPathChars method. |
| ArgumentNullException |
searchPattern is null. |
| DirectoryNotFoundException |
The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive). |
| UnauthorizedAccessException |
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() { 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()); } } }
-
FileIOPermission
for reading from files and directories and for access to the path. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery
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.

