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. 十大javaScript框架

    http://blog.163.com/hongshaoguoguo@126/blog/static/180469812012102645931862/ Prototype特点:一个非常优雅的JS库, ...

  2. Heap堆

    #pragma once#include<iostream>using namespace std;#include<vector> template<class T&g ...

  3. 参照跟老男孩学linux运维搭建nagios实验小结

        nagios效果示例 http://192.168.0.236/nagios       用户名:hong     密码:123   一. 服务端安装准备   1. 更新源 cd /etc/y ...

  4. Oracle数据库中日期/数字和字符之间的转换和计算

    --查出当前系统时间 select SYSDATE from table; --格式转换 -- TO_CHAR 把日期或数字转换为字符串 -- TO_CHAR(number, '格式') -- TO_ ...

  5. c语言-学生成绩信息系统

    #include<stdio.h> #define N 100 int Count=0; struct stu { int num; char name[20]; int computer ...

  6. CRC32算法笔记

    这几天在研究CRC32的计算过程,看了CRC算法的原理,也看了不少通过移位法实现的代码,但是算出的结果跟校验工具算的不一致. 折腾了好长时间,终于找到一个手工计算CRC32的文章,再对照IEEE 80 ...

  7. 题目1022:游船出租(hash简单应用)

    问题来源 http://ac.jobdu.com/problem.php?pid=1022 问题描述 每次输入:船号(1~100) 键值(S或E) 发生时间(小时:分钟).当船号为0时,代表一天结束: ...

  8. js 常用js正则表达式大全

    一.校验数字的js正则表达式   1 数字:^[0-9]*$     2 n位的数字:^d{n}$     3 至少n位的数字:^d{n,}$     4 m-n位的数字:^d{m,n}$     5 ...

  9. Rx

    more detailed in WIKI's document SDP :session description protocal book AAA AA-Answer 鉴权授权应答AAR AA-R ...

  10. DFS-20190206

    找出所有方案 排列和组合问题 排列: https://www.lintcode.com/problem/combination-sum/description public class Solutio ...