static void Main(string[] args)
{
string dir = @"C:\";
string[] dirs=Directory.GetDirectories(dir);
long totalSize = ;
if(dirs!=null && dirs.Any())
{
foreach(string dr in dirs)
{
var size = new DirectoryInfo(dr).GetDirectorySize();
totalSize += size;
Console.WriteLine($"dir:{dr},size:{size}");
}
}
Console.WriteLine($"totalSize:{totalSize}");
System.Diagnostics.Debug.WriteLine($"totalSize:{totalSize}");
} static class DirHelper
{
public static long GetDirectorySize(this System.IO.DirectoryInfo directoryInfo, bool recursive = true)
{
var startDirectorySize = default(long);
try
{ if (directoryInfo == null || !directoryInfo.Exists)
return startDirectorySize; //Return 0 while Directory does not exist. //Add size of files in the Current Directory to main size.
foreach (var fileInfo in directoryInfo.GetFiles())
System.Threading.Interlocked.Add(ref startDirectorySize, fileInfo.Length); if (recursive) //Loop on Sub Direcotries in the Current Directory and Calculate it's files size.
System.Threading.Tasks.Parallel.ForEach(directoryInfo.GetDirectories(), (subDirectory) =>
System.Threading.Interlocked.Add(ref startDirectorySize, GetDirectorySize(subDirectory, recursive))); //Return full Size of this Directory.
}
catch
{ }
return startDirectorySize;
}
}
static void DiskDemo()
{
string dir = @"C:\Windows\";
string[] dirs = Directory.GetDirectories(dir);
long totalSize = ;
StringBuilder builder = new StringBuilder();
List<Dir> dirList = new List<Dir>();
if (dirs != null && dirs.Any())
{
foreach (string dr in dirs)
{
var size = new DirectoryInfo(dr).GetDirectorySize();
Dir d = new Dir();
d.DirName = dr;
d.DirSize = size;
dirList.Add(d);
totalSize += size;
}
} foreach (var dd in dirList.OrderByDescending(x => x.DirSize))
{
System.Diagnostics.Debug.WriteLine(dd);
Console.WriteLine(dd);
}
System.Diagnostics.Debug.WriteLine($"totalSize:{totalSize}");
} class Dir
{
public string DirName { get; set; }
public long DirSize { get; set; } public override string ToString()
{
return $"DirName:{DirName},DirSize:{DirSize}";
}
}

C# calculate disk size的更多相关文章

  1. Powershell Get File/Disk Size

    知识点: 1.获取路径中的文件夹:Get-ChildItem $startFolder  | Where-Object {$_.PSIsContainer -eq $True} | Sort-Obje ...

  2. The commands of Disk

    The commands of Disk fdisk( the disk size is less 2TB) fdisk - partition table manipulator for Linux ...

  3. VirtualBox: Resize a Fedora, CentOS, or Windows Dynamic Guest Virtual Disk (VDI) in VirtualBox

    Here's the scenario: you've set up Dynamically Allocated Storage for the hard drive on your Guest VM ...

  4. disk.go

    package disk import "syscall" //空间使用结构体 type DiskStatus struct {     Size uint64     Used ...

  5. Extend the size of ext3 partition online in VM

    1. Increase disk space in vCenter 2. scan disk on the Linux VM # fdisk -lu > /tmp/fdisk. # > / ...

  6. 《Note --- Unreal --- MemPro (CONTINUE... ...)》

    Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...

  7. Total Commander 8.52 Beta 1

    Total Commander 8.52 Beta 1http://www.ghisler.com/852_b1.php 10.08.15 Release Total Commander 8.52 b ...

  8. [转]C/C++ 实现文件透明加解密

    今日遇见一个开超市的朋友,真没想到在高校开超市一个月可以达到月净利润50K,相比起我们程序员的工资,真是不可同日而语,这个世道啊,真是做程序员不如经商开超市, 我们高科技的从业者,真是造原子弹不如卖茶 ...

  9. 应用matplotlib绘制地图

    #!/usr/bin/env python # -*- coding: utf-8 -*- from math import sqrt import shapefile from matplotlib ...

随机推荐

  1. Linux下如何编辑pdf文件目录

    目前,我使用的是系统LinuxMint,之前硬盘没坏时都是用Foxit Reader和Okular查看pdf文件,可是后来发现Foxit Reader十分吃CPU,而且现在换了固态硬盘之后只有120G ...

  2. java8-StreamAPI之collection归约操作

    一说明 经过前一篇的StreamAPI学习,基本的流操作我相信大家都熟练于心了,那么今天是要详细解析一下收集器(collect)这么API 前提要区分,collect(StreamAPI)与coll ...

  3. 《Java基础知识》Java线程的概念

    按照规划,从本篇开始我们开启『并发』系列内容的总结,从本篇的线程开始,到线程池,到几种并发集合源码的分析,我们一点点来,希望你也有耐心,因为并发这块知识是你职业生涯始终绕不过的坎,任何一个项目都或多或 ...

  4. HTML语法简要总结

    HTML基本语法 认识网页 网页主要由文字.图像和超链接等元素构成.当然,除了这些元素,网页中还可以包含音频.视频以及Flash等. 常见浏览器内核介绍 浏览器是网页运行的平台,常用的浏览器有IE.火 ...

  5. 《Dotnet9》建站-记录建站过程中使用的一些网址

    时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...

  6. Microsemi Libero使用技巧——使用FlashPro生成stp程序文件

    前言 在工程代码编译完成之后,如果需要给某个芯片下载程序时,或者是工厂量产烧录程序时,我们不需要把整个工程文件给别人,而只需要把生成的下载文件给别人,然后使用FlashPro就可以单独下载程序文件了. ...

  7. Internet History,Technology,and Security -Transport Control Protocol(TCP)(Week6)

    Week6 Technology: Transport Control Protocol(TCP) Welcome to Week 6 of IHTS. We are in our second we ...

  8. python内置模块-datetime

    摘录 廖雪峰网站 datetime是python内置的处理日期和时间的标准库 获取当前日期和时间 >>> from datetime import datetime >> ...

  9. Oracle 创建用户,赋予指定表名/视图只读权限

    步骤指南 创建用户 格式:; 语法:create user 用户名 identified by 密码; 注:密码不行的话,前后加(单引号):' create user TEST identified ...

  10. 集合系列 Map(十四):WeakedHashMap

    WeakedHashMap 也是 Map 集合的哈希实现,但其余 HashMap 的不同之处在于.其每个节点的 value 引用是弱引用,可以方便 GC 回收. public class WeakHa ...