Use. NET FrameWork to obtain CPU, memory usage, and disk space

Source: Internet
Author: User

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

 
 
     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.