开发者

How can I get all folders in a drive?

开发者 https://www.devze.com 2023-01-08 07:23 出处:网络
How can I retrieve a list of a开发者_JAVA百科ll folders in a drive in VB.NET?Like this: Directory.GetDirectories(\"C:\\\", \"*\", SearchOption.AllDirectories)

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

精彩评论

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

关注公众号