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 ...
随机推荐
- iOS开发makeKeyAndVisible和makeKeyWindow区别
参考链接:https://www.jianshu.com/p/c7647caa8bd1
- Android 列表对话框 setItems
private Button button; private final CharSequence[] items = { "北京", "上海", " ...
- swoole如何使php永久运行
有需要学习交流的友人请加入交流群的咱们一起,有问题一起交流,一起进步!前提是你是学技术的.感谢阅读! 点此加入该群jq.qq.com soole可以通过开启守护进程使PHP永久运行. 守护进程化.设 ...
- 【转】淘宝UED上关于chrome的transition闪烁问题的解决方案
最近在用BetterScroll实现一个功能的时候,在滚动区域中会有一个绝对定位的按钮,结果在IOS中出现了快速滚动,停止的时候,会先消失后显现的问题,所以查找了相关的文章,发现是transition ...
- Java面向对象之构造器
目录 Java面向对象之构造器 利用构造器确保初始化 构造器重载 Java面向对象之构造器 利用构造器确保初始化 初始化问题是关系编程方式是否安全的一个重要的问题. 功能:在创建对象时执行初始化. 在 ...
- 如何判断IE OCX插件正常安装?
项目中用到了一个第三方的ie ocx控件,而经常遇到客户和测试小伙伴反馈相关功能无法正常使用,也没有友好提示.考虑到这个问题,必须要有一个ie ocx控件的检查机制. 检查原理 创建ActiveXOb ...
- 【BZOJ 2138】stone
Problem Description 话说 \(Nan\) 在海边等人,预计还要等上 \(M\) 分钟.为了打发时间,他玩起了石子. \(Nan\) 搬来了 \(N\) 堆石子,编号为 \(1\) ...
- 【HTML】--- 列表、表格、form表单标签
Html常用标签(2) 上篇博客讲了些常用的html标签 :[HTML]---常用标签(1) 这里主要讲 列表.表格标签和 form表单标签. 一.列表.表格标签 1.列表标签 概念 把内容以列表的形 ...
- wpf 模拟抖音很火的罗盘时钟,附源码,下载就能跑
wpf 模拟抖音很火的罗盘时钟,附源码 前端时间突然发现,抖音火了个壁纸,就是黑底蕾丝~~~ 错错错,黑底白字的罗盘时钟! 作为程序员的我,也觉得很新颖,所以想空了研究下,这不,空下来了就用wpf, ...
- 用Python6种方法:给定一个不超过5位的正整数,判断有几位
方法一:作比较 a=int(input(">>>>")) if a<10: print(1) elif a<100: #第一个条件已经过滤了大于 ...