Home > Backend Development > C#.Net Tutorial > How to obtain the machine's memory and CPU information?

How to obtain the machine's memory and CPU information?

零下一度
Release: 2017-06-23 16:09:20
Original
1992 people have browsed it

I recently worked on a project where I needed to obtain the CPU and memory usage of the machine. I spent some time searching online and did some testing myself. To sum up, there are basically two ways: one is to use WMI (2 types), and the other is to use Performance counter.

1. Use WMI to create connection to the computer passing username and password. Once the connection is created, query the CPU& memory by passing the query, similar as SQL. This way can get CPU & memory for remote PC and local PC. For example:

System.Management.ConnectionOptions Conn = new ConnectionOptions();

Conn.Username = mpusername;

                Conn.Password = mppwd;

           string scopestring = "//" + mpserver + "/root/cimv2";

                       through '''''' ( ); ##                                                                                                                                                                                        mos. , cannot be used, finally found this class "Win32_PerfRawData_PerfOS_Memory"

ManagementObjectCollection mcr = mcp.getQueryResult("select * from Win32_ComputerSystem");

         

                                     {

                                                                                                                                                                               }

        }

ManagementObjectCollection MOC = mcp.getQueryResult ("Select * from Win32_perFrawdata_perfos_memory");

Foreach (Managementobject MoC) {

String avilable = MO.GetPropertyValue ("AvailableBytes"). Tostring ();

avilablem = long.parse (avilation);

}

2. Get Local's CPU and Momory Information Passing The WMI Classes, SUCH As " Win32_Processor", "Win32_OperatingSystem"

ManagementClass mc = new ManagementClass("Win32_OperatingSystem");

    ManagementObjectCollection moc = mc.GetInstances();

3. Use performance counter to get performance data passing the performance counter name, such as monitor.PerformanceCounterFun("Processor", "_Total", "% Processor Time"). Normally we shoud get performance counter data several times, then use the average values.



Although these are relatively simple, I still want to record them. I hope they can be useful to everyone!

The above is the detailed content of How to obtain the machine's memory and CPU information?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template