Export (0) Print
Expand All

DirectoryInfo.GetDirectories Method (String)

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function GetDirectories( _
   ByVal searchPattern As String _
) As DirectoryInfo()
[C#]
public DirectoryInfo[] GetDirectories(
 string searchPattern
);
[C++]
public: DirectoryInfo* GetDirectories(
 String* searchPattern
) [];
[JScript]
public function GetDirectories(
   searchPattern : String
) : DirectoryInfo[];

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.

Exceptions

Exception Type Condition
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.

Remarks

Wild cards 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 are found that match searchPattern, this method returns only the root directory.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
Rename or move a directory. Directory.Move

DirectoryInfo.MoveTo

Delete a directory. Directory.Delete

DirectoryInfo.Delete

Create a directory. CreateDirectory

Directory

Create a subdirectory. CreateSubdirectory
See the files in a directory. Name
See all the files in all subdirectories of a directory. GetFileSystemInfos
Find the size of a directory. Directory
Determine if a file exists. Exists
Determine if a directory exists. Exists

Example

[Visual Basic, C#, C++] The following example counts the directories in a path that contain the specified letter.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di As DirectoryInfo = New DirectoryInfo("c:\")

        Try
            'Get only subdirectories that contain the letter "p."
            Dim dirs As DirectoryInfo() = di.GetDirectories("*p*")
            Console.WriteLine("The number of directories containing the letter p is {0}.", dirs.Length)
            Dim diNext As DirectoryInfo
            For Each diNext In dirs
                Console.WriteLine("The number of files in {0} is {1}", diNext, _
                 diNext.GetFiles().Length)
            Next

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
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());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    try {
        DirectoryInfo* di = new DirectoryInfo(S"c:\\");

        // Get only subdirectories that contain the letter "p."
        DirectoryInfo* dirs[] = di->GetDirectories(S"*p*");
        Console::WriteLine(S"The number of directories containing the letter p is {0}.", __box(dirs->Length));

        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            DirectoryInfo* diNext = __try_cast<DirectoryInfo*>(myEnum->Current);

            Console::WriteLine(S"The number of files in {0} is {1}", diNext, 
                __box(diNext->GetFiles()->Length));
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | DirectoryInfo.GetDirectories Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft