在以前,我們想獲取CPU,內存等信息就不得不借助win32 API來實現。但現在,.NET FrameWork已經把這些API封裝到.NET類庫中了,所以我們可以借助.NET類庫很輕松的獲取這些信息。首先,介紹幾個要用到的類:
PerformanceCounter:表示windows NT性能計數器組件
ComputerInfo:提供用於獲取與計算機內存,已加載程序集,名稱和操作系統有關信息的屬性
DriveInfo:該類位於System.IO命名空間下,提供對有關驅動器信息的訪問
介紹完幾個必要的類,就開始實踐吧:
.cpu = PerformanceCounter(, , .cinf = ComputerInfo();
percentage = Math.Round(percentage,
3.獲取占用內存的百分比
usedMem = .cinf.TotalPhysicalMemory - .cinf .AvailablePhysicalMemory ; )(usedMem / Convert.ToDecimal(.cinf.TotalPhysicalMemory) *
HardDiskInfo GetHardDiskInfoByName(= HardDiskInfo { FreeSpace = GetDriveData(drive.AvailableFreeSpace),TotalSpace =GetDriveData (drive.TotalSize ),Name = IEnumerable<HardDiskInfo><HardDiskInfo> list = List<HardDiskInfo> (DriveInfo d HardDiskInfo { Name = d.Name, FreeSpace = .GetDriveData(d.AvailableFreeSpace), TotalSpace = GetDriveData( data) (data / Convert.ToDouble() / Convert.ToDouble() / Convert.ToDouble()).ToString( HardDiskInfo Name { ; FreeSpace { ; TotalSpace { ;
運行結果:
Demo下載:
demo