开发者

How can I get all folders in a drive?

How can I retrieve a list of a开发者_JAVA百科ll 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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜