C#获取文件夹所占空间大小的功能
作者:天方 时间:2022-12-02 10:54:15
虽然现在硬盘越来越大,但是清理垃圾还是必要的。这时我们往往需要一个获取文件夹所占空间大小的功能,从而判断垃圾文件的位置。
这个时候,我们常用的在右键属性中查看文件夹所占空间的方法显得效率实在太低。往往需要一些工具来辅助实现这个功能。一般有两个工具可以实现这个功能:diruse和du。diruse是MS在系统中的一个附加的工具,du是sysinternals公司的,不过sysinternals好像已经被MS收购了。这两个工具都是命令行工具,但也保持着MS一贯的简单易用的特点。
这里以diruse为例介绍其用法:
diruse /M /* c:\OTHER
Size (mb) Files Directory
2.91 14 SUB-TOTAL: C:\OTHER\BusinessInfo
61.98 1309 SUB-TOTAL: C:\OTHER\software
41.60 41 SUB-TOTAL: C:\OTHER\drivers
0.02 21 SUB-TOTAL: C:\OTHER\work
3.03 9 SUB-TOTAL: C:\OTHER\config
0.00 3 SUB-TOTAL: C:\OTHER\lnetwork
182.16 537 SUB-TOTAL: C:\OTHER\bkup
14.71 6 SUB-TOTAL: C:\OTHER\vpnclient
1.81 60 SUB-TOTAL: C:\OTHER\info
817.20 224 SUB-TOTAL: C:\OTHER\tools
515.25 449 SUB-TOTAL: C:\OTHER\wtnfiles
3089.50 10765 SUB-TOTAL: C:\OTHER\MP3
4730.18 13438 TOTAL
可以非常直观的看到各个文件夹所占的空间。
但是一个非常郁闷的地方是:这两个程序都不支持中文,一旦碰到中文文件夹就晕菜了,无法显示全部路径。作为MS的官方工具,有这个bug确实让人大跌眼镜。没办法,我只好写了个程序来纠正这个bug。
class DirUseInfo
{
public string Path { get; private set; }
public int Percent { get; private set; }
public int FileCount { get; set; }
public long Size { get; set; }
Lazy<DirUseInfo[]> subDirs;
public DirUseInfo[] SubDirs { get { return subDirs.Value; } }
private DirUseInfo(string path, long size, int fileCount,int percent)
{
this.Path = path;
this.Size = size;
this.FileCount = fileCount;
this.Percent = percent;
this.subDirs = fileCount == 0 ? new Lazy<DirUseInfo[]>() : new Lazy<DirUseInfo[]>(() => GetDirUseInfo(path));
}
public override string ToString()
{
return string.Format("[{0}% {1} {2}]", Percent, Size, Path);
}
public static DirUseInfo[] GetDirUseInfo(string dir)
{
var subDirs = Directory.GetDirectories(dir);
var p = Process.Start(new ProcessStartInfo(@"D:\Tools\Du\diruse.exe", @"/* " + dir)
{
UseShellExecute = false,
RedirectStandardOutput = true,
});
p.WaitForExit();
var output = p.StandardOutput.ReadToEnd();
var matches = Regex.Matches(output, @"(\d+|Access Denied)\s+(\d+).+");
if(subDirs.Length!=matches.Count-1) //match最后一项是汇总
throw new InvalidOperationException();
var totalSize = long.Parse(matches[matches.Count - 1].Groups[1].Value);
var dirsUseInfo = new DirUseInfo[subDirs.Length];
for (int i = 0; i < dirsUseInfo.Length; i++)
{
var groups = matches[i].Groups;
var path = subDirs[i];
var fileCount = int.Parse(groups[2].Value);
var size = matches[i].Value.StartsWith("Access Denied") ? 0 : long.Parse(groups[1].Value);
var percent = (int)(size * 100 / totalSize);
dirsUseInfo[i] = new DirUseInfo(path, size, fileCount, percent);
}
return dirsUseInfo;
}
}
原理很简单,仅仅是用C#把diruse的输出封装和匹配了一下,使其生成一个DirUseInfo的对象,利用diruse的输出获取了一些常用的信息。从而可以集成到windows程界面的程序中去。我自己用的时候是用wpf写了一个界面的,不过还不是很令人满意,就不献丑了。这里就仅仅提供一下diruse的封装,以供有同样需求的朋友参考。
来源:https://www.cnblogs.com/TianFang/archive/2010/12/10/1902636.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android通过XListView实现上拉加载下拉刷新功能
c# socket编程udp客户端实现代码分享
Java try-with-resource语法使用解析
Android基于TextView属性android:ellipsize实现跑马灯效果的方法
Android studio 添加assets文件夹的方法
![](https://img.aspxhome.com/file/2023/2/137342_0s.png)
使用Java代码来比较Android客户端版本号
C++实现哈夫曼树编码解码
![](https://img.aspxhome.com/file/2023/9/134769_0s.jpg)
RxJava加Retrofit文件分段上传实现详解
![](https://img.aspxhome.com/file/2023/3/138843_0s.png)
浅谈Java slf4j日志简单理解
Java基于接口实现模拟动物声音代码实例
Android实现九宫格横向左右滑动
![](https://img.aspxhome.com/file/2023/1/138191_0s.jpg)
spring Boot查询数据分页显示的方法实例
java面试题之try中含return语句时代码的执行顺序详解
![](https://img.aspxhome.com/file/2023/3/59913_0s.png)
C#微信公众号开发之服务器配置
![](https://img.aspxhome.com/file/2023/0/100170_0s.png)
java弹幕小游戏1.0版本
IDEA 2021.1 的 Win 和 Mac 快捷键大全
Java线程池用法实战案例分析
elasticsearch分布式及数据的功能源码分析
![](https://img.aspxhome.com/file/2023/7/58087_0s.png)
Android仿微信雷达辐射搜索好友(逻辑清晰实现简单)
![](https://img.aspxhome.com/file/2023/6/137896_0s.gif)