zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

文件计算 目录 所有 路径 数量 或是 某一
2023-09-27 14:21:30 时间

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

 

不管是计算目录或理文件,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