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. Codeforces Round #609 (Div. 2)

    A题 给出n,求大于n的两个合数a和b,并且a-b = n 直接输出n的倍数即可 int n; int main() { cin >> n; cout << 9*n <& ...

  2. Internet History,Technology,and Security -Technology: Application Protocols(Week7)

    Week7 Technology: Application Protocols This week, we’ll be covering application protocols. With rel ...

  3. CentOS7 安装 Redis 并设置开机启动

    1.下载 https://redis.io/download cd /usr/local/src wget -c http://download.redis.io/releases/redis-3.2 ...

  4. 批量SSH key-gen无密码登陆认证脚本

    SSH key-gen无密码登录认证脚本 使用为了让linux之间使用ssh不需要密码,可以采用了数字签名RSA或者DSA来完成.主要使用ssh-key-gen实现. 通过 ssh-key-gen 来 ...

  5. C#基础——break ,continue, return用法

  6. MyBatis进阶讲解+ssm集成

    1.sql映射器Mapper MyBatis基于动态代理机制,让我们无需再编写Dao的实现. 传统Dao接口,现在名称统一以Mapper结尾,还有我们映射器配置文件要和映射器在同一个包. 1.1使用映 ...

  7. Zabbix Server 3.2

    软件环境 Centos7.3 LAMP Zabbix 3.2  1. Installing repository configuration package Install the repositor ...

  8. 去除TextView设置lineSpacingExtra后,最后一行多出的空白

    转载请标明出处:https://www.cnblogs.com/tangZH/p/11985745.html 有些手机中,给TextView设置lineSpacingExtra后会出现最后一行的文字也 ...

  9. 如何减小ABAP业务代码的复杂度

    在程序开发的过程中,相同的功能往往有不同的实现方式.对于可以实现同样功能的不同代码,复杂度是用于比较其质量优劣的重要指标. 在本文中,代码复杂度是指代码被理解/修改的难易程度.越容易被理解.修改的代码 ...

  10. ElasticSearch - 遐想

    众所周知,ElasticSearch 存在一个问题,无法查询最近 1s 的写入.近实时这个属性,限制了其在某些场景的应用.本文记录了我在日常工作中想到的,解决特定场景的一些方案.既然是记录,便会有好有 ...