ARTICLE

Get Sub Directories in C#

Posted by Dinesh Beniwal Articles | Files, Directory, IO July 20, 2012
In this article we will discuss about how to get sub directories in C#.
Reader Level:

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#

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts