欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#獲取文件夾所占空間大小的功能

 更新時間:2022年06月18日 09:33:11   作者:天方  
這篇文章介紹了C#獲取文件夾所占空間大小的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

雖然現在硬盤越來越大,但是清理垃圾還是必要的。這時我們往往需要一個獲取文件夾所占空間大小的功能,從而判斷垃圾文件的位置。

這個時候,我們常用的在右鍵屬性中查看文件夾所占空間的方法顯得效率實在太低。往往需要一些工具來輔助實現這個功能。一般有兩個工具可以實現這個功能:diruse和du。diruse是MS在系統(tǒng)中的一個附加的工具,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的封裝,以供有同樣需求的朋友參考。

到此這篇關于C#獲取文件夾所占空間大小的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論