Export (0) Print
Expand All

DirectoryInfo.GetDirectories Method

Returns the subdirectories of the current directory.

Overload List

Returns the subdirectories of the current directory.

Supported by the .NET Compact Framework.

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

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

Supported by the .NET Compact Framework.

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

Example

[Visual Basic, C#, C++] The following example counts the directories in a path that contain 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()
        ' 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.

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace

Show:
© 2014 Microsoft