List all subfolders in C#

Use the following code to list all subdirectories of a folder.

try
{
    String[] allfolders = System.IO.Directory.GetDirectories("C:\", "*", 
	System.IO.SearchOption.TopDirectoryOnly);
    if (allfolders.Length == 0)
	Console.WriteLine("empty");
    else
	foreach (String folder in allfolders)
		Console.WriteLine(folder.Substring(folder.LastIndexOf("\") + 1) );
}
catch (Exception)
{
    //...
}
%d bloggers like this: