Number of folder inside a directory

后端 未结 3 1605
被撕碎了的回忆
被撕碎了的回忆 2020-12-10 15:59

How do I know the number of folders inside a directory?

I try using System.IO.Directory but no luck.

3条回答
  •  执念已碎
    2020-12-10 16:35

    You've got a couple of options:

    int directoryCount = System.IO.Directory.GetDirectories(@"c:\yourpath\").Length
    

    or

    var directoryInfo = new System.IO.DirectoryInfo(@"c:\yourpath\");
    int directoryCount = directoryInfo.GetDirectories().Length;
    

    If you need to do other things with them, and you're using .NET 4, you can use the DirectoryInfo.EnumerateDirectories() function for performance reasons as well.

    So yeah, lots of options. If you're still having problems, you might want to let us know what didn't work when using System.IO.Directory.

提交回复
热议问题