public void RealTimeHardDisk() { Task.Run(() => { try { while (true) { foreach (var item in objDriveInfo) { string diskName = item.Name; var hardDisk = HardDisks.Find(p => p.DiskName == diskName); if (hardDisk != null) { hardDisk.Available = AnyHelper.BytesToString(item.AvailableFreeSpace); if (item.AvailableFreeSpace < this.DiskLimitSize) { hardDisk.Tip = "磁盘空间不足"; AnyHelper.Alarm(); App.Current.Dispatcher.BeginInvoke((Action) delegate() { this.ShowNotification("警告", "请注意,磁盘控件不足,请及时清理"); }); } else { hardDisk.Tip = "正常"; } hardDisk.UsedRate = hardDisk.UsedRate = 100 - (int)(item.AvailableFreeSpace / (item.TotalSize * 1.00) * 100); } Thread.Sleep(2000); } } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } }); }
private void InitDrivers() { try { objDriveInfo = DriveInfo.GetDrives(); foreach (var item in objDriveInfo) { var hardDisk = new HardDisk(); hardDisk.DiskName = item.Name; hardDisk.Available = AnyHelper.BytesToString(item.AvailableFreeSpace); hardDisk.Total = AnyHelper.BytesToString(item.TotalSize); hardDisk.UsedRate = 100 - (int)(item.AvailableFreeSpace / (item.TotalSize * 1.00) * 100); hardDisk.Tip = "正常"; HardDisks.Add(hardDisk); } } catch (Exception ex) { LogHelper.WriteLog(ex.Message); } }