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. 利用Python工具进行打包功能

    基于Python脚本 iOS 工程的自动打包 导入的库 import os import requests import webbrowser import subprocess import shu ...

  2. [转] iOS中@class #import #include 简介

    [转载自:http://blog.csdn.net/chengwuli125/article/details/9705315] 一.解析        很多刚开始学习iOS开发的同学可能在看别人的代码 ...

  3. 搭建gogs

    https://blog.csdn.net/hwm_life/article/details/82969005 服务器环境 CentOS 7 64位 安装Gogs所需的其它环境 需要安装的依赖有Ngi ...

  4. print高亮显示

    显示颜色格式:\033[显示方式;字体色;背景色m......[\033[0m] ------------------------------------------- --------------- ...

  5. C#集合之不变的集合

    如果对象可以改变其状态,就很难在多个同时运行的任务中使用.这些集合必须同步.如果对象不能改变器状态,就很容易在多个线程中使用. Microsoft提供了一个新的集合库:Microsoft Immuta ...

  6. API Monitor程序分析工具简介

    API Monitor是一个免费软件,可以让你监视和控制应用程序和服务,取得该应用程序的API调用情况. 它是一个强大的工具,看到的应用程序和服务是如何工作的,或跟踪,你在自己的应用程序的问题. AP ...

  7. pymysql 各种坑总结

    pymysql各种坑只针对自己的项目1.关于关闭连接,报错为:pymysql.err.InterfaceError: (0, '') 这个错误原因:对已经关闭的链接再次进行操作,参考MySQL.err ...

  8. 导出excel设置样式(Aspose.Cells)

    Aspose.Cells.Style style = xlBook.Styles[xlBook.Styles.Add()];style1.Pattern = Aspose.Cells.Backgrou ...

  9. mysql安装启动 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

    首次安装mysql 启动 mysql -uroot 以下错误: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using ...

  10. oracle mysql的序列的新增、删除、修改及使用

    序列的使用  参考文献: https://blog.csdn.net/meijory/article/details/51891529 1.序列介绍 序列: 是 oracle 提供的用于产生一系列唯一 ...