C# calculate disk size
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的更多相关文章
- Powershell Get File/Disk Size
知识点: 1.获取路径中的文件夹:Get-ChildItem $startFolder | Where-Object {$_.PSIsContainer -eq $True} | Sort-Obje ...
- The commands of Disk
The commands of Disk fdisk( the disk size is less 2TB) fdisk - partition table manipulator for Linux ...
- 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 ...
- disk.go
package disk import "syscall" //空间使用结构体 type DiskStatus struct { Size uint64 Used ...
- 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. # > / ...
- 《Note --- Unreal --- MemPro (CONTINUE... ...)》
Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...
- 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 ...
- [转]C/C++ 实现文件透明加解密
今日遇见一个开超市的朋友,真没想到在高校开超市一个月可以达到月净利润50K,相比起我们程序员的工资,真是不可同日而语,这个世道啊,真是做程序员不如经商开超市, 我们高科技的从业者,真是造原子弹不如卖茶 ...
- 应用matplotlib绘制地图
#!/usr/bin/env python # -*- coding: utf-8 -*- from math import sqrt import shapefile from matplotlib ...
随机推荐
- C# 中获取一个目录下的目录与文件
//获得目录下所有文件和子目录使用DirectoryInfo类的GetFileSystemInfos()方法. //获得目录下所有目录 string[] dirs = Directory.GetDir ...
- Python学习之编码
Python2默认解释器的编码:ascii: Python3默认解释器的编码:UTF-8 ascii码:只会识别英文字母.数字和标点.8位表示一个英文字符,1个字节 万国码Uicode:目前的所有语言 ...
- SAP QM 检验批里样品数量的确定
SAP QM 检验批里样品数量的确定 如下的检验批890000045939, 样品数量是50 PC. 检查该检验批对应的检验计划, 这些检验特性都有自己的取样策略,相关的取样数量,体现在结果录入界面, ...
- Android 中 MessageQueue 的 nativePollOnce
Android SDK 中的事件循环已经是一个老生常谈的问题了, 像 Handler Looper MessageQueue 这几个类也是被大家研究透彻了. 但是再回头看以前自己的分析, 总感觉差点什 ...
- kotlin之变量的可空与非空
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/218 kotlin之变量的可空与非空 上面一篇文章,介绍了 ...
- VMware 虚拟机黑屏问题
VMware Workstation 14打开虚拟机黑屏解决方法 听语音 浏览:0 | 更新:2017-11-21 16:56 | 标签:操作系统 虚拟机 VMWARE 1 2 3 4 分步阅读 最近 ...
- 数据结构学习--双向链表(python)
概念 双向链表(Double_linked_list)也叫双链表,是链表的一种,它的每个数据结点中都有 两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可 以很方便地访问 ...
- Promise代码详解(show you the code)
认识异步函数 同步函数: const sum1 =(a,b)=>{ return a+b } console.log('AAA'); console.log(sum(5,6)); conosle ...
- ASP.NET MVC5基础-过滤器(Filters)详解
什么是过滤器? 过滤器的类型与作用 定义过滤器 授权过滤器 动作过滤器 结果过滤器 异常处理过滤器 过滤器的使用方法 总结 什么是过滤器? 通过上一篇关于Controller控制器的文章我们知道,MV ...
- C++ STL容器
不定长数组:vector vector是一个模板类,作用为创建一个不定长的数组 声明方式:vector<int>a或者vector<double>b这种类型的方式. 基本操作: ...