Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DirectoryInfo.GetDirectories Method

Returns the subdirectories of the current directory.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public DirectoryInfo[] GetDirectories()

Return Value

Type: System.IO.DirectoryInfo[]
An array of DirectoryInfo objects.

ExceptionCondition
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.

UnauthorizedAccessException

The caller does not have the required permission.

If there are no subdirectories, this method returns an empty array. This method is not recursive.

This method pre-populates the values of the following DirectoryInfo properties:

The following example retrieves all the directories in the root directory and displays the directory names.

using System;
using System.IO;

public class GetDirectoriesTest 
{
    public static void Main() 
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("c:\\");

        // Get a reference to each directory in that directory.
        DirectoryInfo[] diArr = di.GetDirectories();

        // Display the names of the directories. 
        foreach (DirectoryInfo dri in diArr)
            Console.WriteLine(dri.Name);
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Show:
© 2015 Microsoft