Count Directory recursively in CSharp
Description
The following code shows how to count Directory recursively.
Example
/*w w w . j ava 2s . c om*/
using System;
using System.IO;
public class DirectoryCounter {
public static void Main() {
DirectoryInfo myDir = new DirectoryInfo(@"c:\Program Files");
int total = CountDirectories(myDir);
Console.WriteLine(total);
}
public static int CountDirectories(DirectoryInfo dir) {
DirectoryInfo[] dirs = dir.GetDirectories();
int count = dirs.Length;
foreach (DirectoryInfo subDir in dirs) {
count += CountDirectories(subDir);
}
return count;
}
}