In the past, we had to use win32 API to obtain information such as CPU and memory. But now, the. NET FrameWork has encapsulated these Apis into the. NET class library, so we can easily obtain this information using the. NET class library. First, we will introduce several classes to be used:
PerformanceCounter: indicates the windows NT Performance Counter component.
ComputerInfo: provides properties for obtaining information related to computer memory, loaded assembly, name, and operating system.
DriveInfo: this class is located in the System. IO namespace and provides access to drive information.
After introducing several necessary classes, let's start to practice:
.cpu = PerformanceCounter(, , .cinf = ComputerInfo();
percentage = Math.Round(percentage,
3. Get the percentage of memory occupied
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 { ;
Running result:
Download Demo:
Demo