CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

How to get all Directories(folders) and Sub directories(sub folders) under a Directory in C#?
Submitted By Bala Murugan
On 11/1/2010 10:14:59 AM
Tags: C#,CodeDigest  

How to get all Directories(folders) and Sub directories(sub folders) under a Directory in C#?

 

Sometimes, we may get requirements to get all directory and subdirectory under a specific directory.

 

We can use GetDirectories() method in Directory class with the wildcard character "*". This wildcard will list all the directoried under the current directory.

 

The following little code snippet will help us to do the same.

 

string[] dirs = Directory.GetDirectories(@"E:\Technicals\Samples","*",SearchOption.AllDirectories);
foreach (string dir in dirs)
        {
Response.Write(dir + "<br>");
        }

 

The above code will print all the directories and sub directories under the folder E:\Technicals\Samples.

You need to include System.IO namespace for the code to work.

 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..