C#獲取文件夾所占空間大小的功能
雖然現(xiàn)在硬盤越來越大,但是清理垃圾還是必要的。這時我們往往需要一個獲取文件夾所占空間大小的功能,從而判斷垃圾文件的位置。
這個時候,我們常用的在右鍵屬性中查看文件夾所占空間的方法顯得效率實在太低。往往需要一些工具來輔助實現(xiàn)這個功能。一般有兩個工具可以實現(xiàn)這個功能:diruse和du。diruse是MS在系統(tǒng)中的一個附加的工具,du是sysinternals公司的,不過sysinternals好像已經(jīng)被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寫了一個界面的,不過還不是很令人滿意,就不獻(xiàn)丑了。這里就僅僅提供一下diruse的封裝,以供有同樣需求的朋友參考。
到此這篇關(guān)于C#獲取文件夾所占空間大小的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中將xml文件反序列化為實例時采用基類還是派生類的知識點討論
在本篇文章里小編給大家整理的是關(guān)于C#中將xml文件反序列化為實例時采用基類還是派生類的知識點討論,有需要的朋友們學(xué)習(xí)下。2019-11-11
深入C#中使用SqlDbType.Xml類型參數(shù)的使用詳解
本篇文章是對在C#中使用SqlDbType.Xml類型參數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# IEnumerable和IEnumerator接口淺析
本文主要介紹了C#中IEnumerable和IEnumerator接口的相關(guān)知識,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02

