GetDirectories Method

Directory.GetDirectories Method

Gets the names of subdirectories in the specified directory.

Overload List

Gets the names of subdirectories in the specified directory.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function GetDirectories(String) As String()
[C#] public static string[] GetDirectories(string);
[C++] public: static String* GetDirectories(String*) __gc[];
[JScript] public static function GetDirectories(String) : String[];

Gets an array of directories matching the specified search pattern from the current directory.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function GetDirectories(String, String) As String()
[C#] public static string[] GetDirectories(string, string);
[C++] public: static String* GetDirectories(String*, String*) __gc[];
[JScript] public static function GetDirectories(String, String) : String[];

Example

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

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetDirectories. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            ' Only get subdirectories that begin with the letter "p."
            Dim dirs As String() = Directory.GetDirectories("c:\", "p*")
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length)
            Dim dir As String
            For Each dir In dirs
                Console.WriteLine(dir)
            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 
        {
            // Only get subdirectories that begin with the letter "p."
            string[] dirs = Directory.GetDirectories(@"c:\", "p*");
            Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
            foreach (string dir in dirs) 
            {
                Console.WriteLine(dir);
            }
        } 
        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 {
        // Only get subdirectories that begin with the letter "p."
        String* dirs[] = Directory::GetDirectories(S"c:\\", S"p*");
        Console::WriteLine(S"The number of directories starting with p is {0}.", __box(dirs->Length));
        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            Console::WriteLine(myEnum->Current);
        }
    } 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.

See Also

Directory Class | Directory Members | System.IO Namespace

Show:
© 2016 Microsoft