Reader Level:
Articles

Get Sub Directories in C#

By Dinesh Beniwal on July 20, 2012
In this article we will discuss about how to get sub directories in C#.
  • 0
  • 0
  • 16641

The GetDirectories method of the Directory class loads all the subdirectories of a directory. To get all subdirectories, we can read subdirectories recursively.

public void GetSubDirectories()

{

    string root = @"C:\Temp";

    // Get all subdirectories

    string[] subdirectoryEntries = Directory.GetDirectories(root);

    // Loop through them to see if they have any other subdirectories

    foreach (string subdirectory in subdirectoryEntries)

        LoadSubDirs(subdirectory);

}

private void LoadSubDirs(string dir)

{

    Console.WriteLine(dir);

    string[] subdirectoryEntries = Directory.GetDirectories(dir);

    foreach (string subdirectory in subdirectoryEntries)

    {

        LoadSubDirs(subdirectory);

    }

}
 

Download free book: Working with Directories in C#

Dinesh Beniwal

 I am working as VP Content Manager, responsible for content publishing, content development, and social relation. You can follow me on twitter @dbeniwal21

COMMENT USING

Trending up