How can I retrieve a list of all folders in a drive in VB.NET?
+3
A:
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
2010-07-20 19:39:13
+1
A:
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;
}
}
}
Dan Tao
2010-07-20 20:16:55