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. 深度剖析YOLO系列的原理

    深度剖析YOLO系列的原理 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-further/p/12072225.html 目录 1. ...

  2. jquery获取表单中的数据

               <form>                 <input name="username" type="text"/&g ...

  3. 代码的鲁棒性:链表中倒数第k个结点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.例如:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从 ...

  4. C#程序编写高质量代码改善的157个建议【16-19】[动态数组、循环遍历、对象集合初始化]

    前言   软件开发过程中,不可避免会用到集合,C#中的集合表现为数组和若干集合类.不管是数组还是集合类,它们都有各自的优缺点.如何使用好集合是我们在开发过程中必须掌握的技巧.不要小看这些技巧,一旦在开 ...

  5. 内网渗透教程大纲v1.0

    内网渗透 ☉MS14-068(CVE-2014-6324)域控提权利用及原理解析 ☉域控权限提升PTH攻击 未完待续...

  6. spring boot 2 + shiro 实现简单的身份验证例子

    Shiro是一个功能强大且易于使用的Java安全框架,官网:https://shiro.apache.org/. 主要功能有身份验证.授权.加密和会话管理.其它特性有Web支持.缓存.测试支持.允许一 ...

  7. Navicat Premium从远程Mysql数据库复制到本地数据库的方法

    之前做在本地测试总是先去现在下一份数据库的sql,然后再在本地建一个相同的,导入sql.然后觉得有点麻烦,现在发现了一个直接远程直接复制一份到本地的方法 工具:Navicat Premium 1.先连 ...

  8. Shell(五):函数

    linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. shell中函数的定义格式: [ function ] funname [()] { action; [return i ...

  9. 富士通 DX90 S2存储分配映射盘

    屁话不多说,直接开操: 1.连接存储 用pc连接到存储管理口,如192.168.1.101 登录管理账号:默认root/root 创建Volume, 点击create开始创建新的volume 按需填写 ...

  10. ReactNative: 使用AppReistry注册类

    一.简介 每一个应用程序的运行都有一个入口文件或者入口函数,例如iOS中的使用UIApplicationMain类完成入口函数的实现,在React-Native中,AppRegistry类就肩负着这个 ...