private long GetDirectorySizeMethod1(string directory)
{
  long directorySize = 0;
  DirectoryInfo di = new DirectoryInfo(directory);
  if (!di.Exists)
  {
    Console.WriteLine("Directory {0} is not exist!", directory);
    return 0;
  }
  foreach (FileInfo fi in di.GetFiles())
  {
  directorySize += fi.Length;
  }
  DirectoryInfo[] dirs = di.GetDirectories();
  foreach (DirectoryInfo sondir in dirs)
  {
    directorySize += GetDirectorySizeMethod1(sondir.FullName);
  }
  return directorySize;
}
private long GetDirectorySizeMethod2(string directory)
{
  long directorySize = 0;
  if (File.Exists(directory))
  {
    FileInfo fi = new FileInfo(directory);
    return fi.Length;
  }
  else
  {
    string[] strs = Directory.GetFileSystemEntries(directory);
    foreach (string str in strs)
    {
      directorySize += GetDirectorySizeMethod2(str);
    }
  }
  return directorySize;
}

c# 获取文件夹大小的更多相关文章

  1. Linux C++获取文件夹大小

    项目中要计算指定文件夹的大小.百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628方法可行,运行正确. 拿到我们的项目中,却遇到一些问题:程序 ...

  2. 用C#实现获取文件夹大小的源代码

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  3. 【VBS】获取文件夹大小

    文件截图: 运行结果: 第一步:编写脚本 GetFloderSize.vbs 1 '获得文件夹的大小 by 王牌飞行员(https://www.cnblogs.com/KMould/p/1233481 ...

  4. python 获取文件夹大小

    __author__ = 'bruce' import os from os.path import join,getsize def getdirsize(dir): size=0l for (ro ...

  5. C#获取文件和文件夹大小

    代码如下: /// <summary> /// 获取文件夹大小 /// </summary> /// <param name="dirPath"> ...

  6. python3获取文件及文件夹大小

    获取文件大小 os.path.getsize(file_path):file_path为文件路径 >>> import os >>> os.path.getsize ...

  7. python 获取文件和文件夹大小

    1.os.path.getsize可以获取文件大小 >>> import os >>> file_name = 'E:\chengd\Cd.db' >> ...

  8. python获取文件及文件夹大小

    Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsi ...

  9. android 获取文件夹、文件的大小 以B、KB、MB、GB 为单位

    android 获取文件夹.文件的大小 以B.KB.MB.GB 为单位   public class FileSizeUtil { public static final int SIZETYPE_B ...

随机推荐

  1. ios 字符串处理:截取字符串、匹配字符串、分隔字符串

    1.截取字符串 NSString*string =@"sdfsfsfsAdfsdf";string = [string substringToIndex:7];//截取掉下标7之后 ...

  2. 模块sys, os, glob, pickle, subprocess常见用法

    参考python常用标准库 http://blog.51cto.com/lizhenliang/1872538 一. sys   1. sys.argv 脚本名1.py, 命令行中执行python 1 ...

  3. String字符串补位

    String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的读者应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重载形式. l    ...

  4. Laravel 视图调用model方法

    首先控制器 model 视图

  5. UML-6.1-用例-示例

    1.总览要点:用例.摘要.非正式.详述.测试用例.用例分析与迭代联系起来. 2.示例:Process Sale 1).客户携带所购商品到达收银台. 2).收银员使用pos系统记录每件商品. 操作契约: ...

  6. 使用Maven运行测试提示Module sdk 1.5的解决方法

    解决方法: 1. 配置Project Structure 2. 在MAVEN_HOME/conf/setting.xml中添加profile 3. 在Maven项目的pom.xml文件里添加标签 三种 ...

  7. linux新增特性timerfd

    https://blog.csdn.net/shreck66/article/details/49745149

  8. (转)Linux服务器SNMP常用OID

    原文:https://www.haiyun.me/archives/linux-snmp-oid.html 收集整理一些Linux下snmp常用的OID,用做服务器监控很不错.服务器负载: 1 2 3 ...

  9. springboot和quartz整合分布式多节点

    虽然单个Quartz实例能给予我们很好的任务job调度能力,但它不能满足典型的企业需求,如可伸缩性.高可靠性满足.假如你需要故障转移的能力并能运行日益增多的 Job,Quartz集群势必成为你应用的一 ...

  10. Docker搭建tomcat运行环境(Dockerfile方式)

    上一篇文章的基本做法是通过centOS的官方镜像启动一个容器,然后进入到容器中,手动敲命令安装JDK跟tomcat,这个跟在linux下搭建没有什么区别,只是用来熟悉docker命令,并且在日常开发中 ...