using System.IO; DirectoryInfo dirInfo = new DirectoryInfo("C:\\MyDirectory"); int numOfDirectories = dirInfo.GetDirectories().Length; Console.WriteLine("The number of directories in {0} is: {1}", dirInfo.FullName, numOfDirectories);
using System.IO; DirectoryInfo dirInfo = new DirectoryInfo("C:\\MyDirectory"); int numOfDirectories = dirInfo.EnumerateDirectories().Count(); Console.WriteLine("The number of directories in {0} is: {1}", dirInfo.FullName, numOfDirectories);This example is similar to the first example but uses the EnumerateDirectories method instead of GetDirectories. The Count extension method is then used to count the number of directories returned by the method. Both these examples use the System.IO namespace, which is a part of the .NET Framework Class Library.