Tutorial :How can I get all folders in a drive?


How can I retrieve a list of all folders in a drive in VB.NET?


Like this:

Directory.GetDirectories("C:\", "*", SearchOption.AllDirectories)  

Note that it will be very slow.
In .Net 4.0, you can make it much faster by changing GetDirectories to EnumerateDirectories.


SLaks's answer is the obvious approach.

If you don't have .NET 4.0 but you also want to mitigate the slowness somewhat, you could write your own recursive function to start lazily enumerating through the directories recursively.

static IEnumerable<DirectoryInfo> GetAllDirectories(DirectoryInfo directory)  {      DirectoryInfo[] directories = directory.GetDirectories();      if (directories.Length == 0)          yield break;        foreach (DirectoryInfo subdirectory in directories)      {          yield return subdirectory;          foreach (DirectoryInfo subsubdirectory in GetAllDirectories(subdirectory))          {              yield return subsubdirectory;          }      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »