C# Program to Get the List of Sub-Directories of a Given Directory
Last Updated :
04 Mar, 2022
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#
using System;
using System.IO;
class GFG{
static void Main()
{
String []all = Directory.GetDirectories( "C:/vignan" );
for ( int i = 0; i < all.Length; i++)
{
Console.WriteLine(all[i]);
}
}
}
|
Output:
C:/vignan\Hello
C:/vignan\Data
C:/vignan\HPink\whalello
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...