ITPub博客

首页 > IT职业 > IT生活 > 计算某一路径下的所有目录或是文件数量

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

IT生活 作者:libinbin 时间:2021-09-09 09:46:22 0 删除 编辑

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

 

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

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/359/viewspace-2812001/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: GridView分割线
请登录后发表评论 登录
全部评论

注册时间:2020-07-23

  • 博文量
    0
  • 访问量
    946