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.
|