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
)
public DirectoryInfo[] GetDirectories (
	String searchPattern
)
public function GetDirectories (
	searchPattern : String
) : DirectoryInfo[]
Not applicable.

Parameters

searchPattern

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

Return Value

An array of type DirectoryInfo matching searchPattern.

Exception typeCondition

ArgumentNullException

searchPattern is a null reference (Nothing in Visual Basic).

DirectoryNotFoundException

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

SecurityException

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

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        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}.", (Int32)dirs.length);

            DirectoryInfo diNext = null;
            for ( int iCtr=0; iCtr < dirs.length ; iCtr++ ) {
                diNext = dirs[iCtr];
                Console.WriteLine("The number of files in {0} is {1}",
                    diNext, (Int32)diNext.GetFiles().length);
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft