磁盘使用情况分析

仿照Ubuntu下的磁盘使用情况分析器用WPF做了个类似的软件,参考https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.2.0/samples/features/Features.WPF使用Caliburn.Micro框架完成

磁盘使用情况分析_第1张图片

 

项目地址: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(Children);
            Children.Clear();
            Children.AddRange(tmp.OrderByDescending(x => x.Size)); //按文件夹大小排序
            Parent.Size += Size;
        }                                                                                                                                                                              
    }                                                                                                                                                                                  
}                                                                                                                                                                                      

 

转载于:https://www.cnblogs.com/ALittleDruid/p/11442185.html

你可能感兴趣的:(运维)