计算某一路径下的所有目录或是文件数量

计算某一路径下的所有目录或是文件数量,可以参考下面代码示例:

 

不管是计算目录或理文件,2个方法均带2个参数,前者是传入的是物理路径,第二个传的是布尔值的参数,即是说可以计算是否包含子目录。

目录:

 public static int DirectoryCount(string physicalPath, bool isIncludeSubDirectory)
        {
            int count = 0;
            if (isIncludeSubDirectory)
                count = Directory.GetDirectories(physicalPath, "*", SearchOption.AllDirectories).Length;
            else
                count = Directory.GetDirectories(physicalPath, "*", SearchOption.TopDirectoryOnly).Length;
            return count;
        }
Source Code


文件:

 public static int FileCount(string physicalPath, bool isIncludeSubDirectory)
        {
            int count = 0;
            if (isIncludeSubDirectory)
                count = Directory.GetFiles(physicalPath, "*.*", SearchOption.AllDirectories).Length;
            else
                count = Directory.GetFiles(physicalPath, "*.*", SearchOption.TopDirectoryOnly).Length;
            return count;
        }
Source Code

 

posted @ 2017-04-23 23:05  Insus.NET  阅读(505)  评论(0编辑  收藏  举报