Export (0) Print
Expand All

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
)

Parameters

searchPattern
Type: System.String

The search string, such as "System*", used to search for all directories beginning with the word "System".

Return Value

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

ExceptionCondition
ArgumentException

searchPattern contains invalid characters. To determine the invalid characters, use the GetInvalidPathChars method.

ArgumentNullException

searchPattern is null.

DirectoryNotFoundException

The path encapsulated in the DirectoryInfo object is invalid, such as being 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.

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

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft