仿照Ubuntu下的磁碟使用情況分析器用WPF做了個類似的軟體,參考https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.2.0/samples/features/Features.WPF使用Caliburn.Micro框架完成 項目地址:h ...
仿照Ubuntu下的磁碟使用情況分析器用WPF做了個類似的軟體,參考https://github.com/Caliburn-Micro/Caliburn.Micro/tree/3.2.0/samples/features/Features.WPF使用Caliburn.Micro框架完成
項目地址: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; } } }