Skip to content
Related Articles

Related Articles

C# Program to Get the List of Sub-Directories of a Given Directory

Improve Article
Save Article
  • Last Updated : 04 Mar, 2022
Improve Article
Save Article

Given a director, now we will find the list of the sub-directories present in the given directory. So to this task, we use the GetDirectories() method of the Directory class. This method is used to get the list of directories/sub-directories from the given directory or sub-directories. We have to specify the directory name to get the sub-directories. Here we are going to return the sub-directories along with their paths. The overloaded methods of this 

Syntax:

public static string[] GetDirectories (string path);DirectoryInfo.GetDirectories Method (System.IO) | Microsoft Docs

2. GetDirectories (String, String): It will return the names of sub-directories (including their paths) that match the specified search pattern in the specified directory.

Syntax:

public static string[] GetDirectories (string path, string searchPattern);

3. GetDirectories (String, String, SearchOption): It will return the names of sub-directories(including their paths) that match the specified search pattern and enumeration options in the specified director option

public static string[] GetDirectories (string path, string searchPattern, System.IO.EnumerationOptions enumerationOptions); string

4. GetDirectories (String, String, SearchOption): It will return the names of sub-directories (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.

Syntax:

public static string[] GetDirectories (string path, string searchPattern, System.IO.SearchOption searchOption);

Example:

C#




// C# program to find the list of sub-directories
// of a given directory
using System;
using System.IO;
 
class GFG{
     
static void Main()
{
     
    // Get the vignan directory path from C drive
    String []all = Directory.GetDirectories("C:/vignan");
     
    // Get all the paths of sub directories
    // present in vignan
    for(int i = 0; i < all.Length; i++)
    {
         
        // Display result
        Console.WriteLine(all[i]);
    }
}
}


Output:

C:/vignan\Hello
        C:/vignan\Data
        C:/vignan\HPink\whalello
       
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!