磁盤使用情況分析


仿照Ubuntu下的磁盤使用情況分析器用WPF做了個類似的軟件,參考https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.2.0/samples/features/Features.WPF使用Caliburn.Micro框架完成

Ubuntu下的磁盤使用情況分析器

 

項目地址:https://github.com/ALittleDruid/DiskSpaceAnalyse

 

核心代碼

public void Analyse()                                                                                                                                                                  
{                                                                                                                                                                                      
    if (Parent != null && !string.IsNullOrEmpty(FolderName) && Directory.Exists(FolderName))                                                                                           
    {                                                                                                                                                                                  
        DirectoryInfo di = new DirectoryInfo(FolderName);                                                                                                                              
        if ((di.Attributes & FileAttributes.Directory) != 0)                                                                                                                           
        {                                                                                                                                                                              
            try                                                                                                                                                                        
            {                                                                                                                                                                          
                var files = di.GetFiles();
                var dirs = di.GetDirectories(); //訪問部分沒有權限的目錄會異常,捕獲異常后忽略即可
                FileCount = files.Length;                                                                                                                                              
                FolderCount = dirs.Length;                                                                                                                                             
                foreach (FileInfo fi in files)                                                                                                                                         
                {                                                                                                                                                                      
                    Size += fi.Length;                                                                                                                                                 
                }                                                                                                                                                                      
                foreach (var item in dirs)                                                                                                                                             
                {                                                                                                                                                                      
                    if ((item.Attributes & FileAttributes.ReparsePoint) != 0)                                                                                                          
                    {                                                                                                                                                                  
                        continue; //忽略符號鏈接,否則可能導致無限遞歸下去               
                    }                                                                                                                                                                  
                    var d = new FolderTreeModel(item.FullName, this);                                                                                                                  
                    Children.Add(d);                                                                                                                                                   
                }                                                                                                                                                                      
                foreach (var item in Children)                                                                                                                                         
                {                                                                                                                                                                      
                    item.Analyse(); //無需擔心遞歸層數過多導致stack overflow
                }                                                                                                                                                                      
            }                                                                                                                                                                          
            catch                                                                                                                                                                      
            {                                                                                                                                                                          
            }                                                                                                                                                                          
            var tmp = new List<FolderTreeModel>(Children);
            Children.Clear();
            Children.AddRange(tmp.OrderByDescending(x => x.Size)); //按文件夾大小排序
            Parent.Size += Size;
        }                                                                                                                                                                              
    }                                                                                                                                                                                  
}                                                                                                                                                                                      

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM