Now I'll explain in the future how to use WMI (Windows Management instrumentation) in C # to get hardware information for your computer.
Our goal is to use the WMI API to get the following information for the computer under C #:
Number of physical processors
Number of logical processors
Number of digits
System architecture
Number of cores
Create a console application in Visual Studio, right-click the reference and select Add Reference, and then select System.Management.
Now that you have included system.management with the using statement, you can use the WMI class reference in your code.
Here is the complete code for generating hardware information.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.threading.tasks;namespace consoleapplication8{class Program {Static voidMain (string[] args) {getcpudetails (); }Private Static void getcpudetails() {foreach(varIteminch NewSystem.Management.ManagementObjectSearcher ("SELECT * from Win32_ComputerSystem"). Get ()) {Console.WriteLine ("Number of physical processors: {0}", item["NumberOfProcessors"]); Console.WriteLine ("Number of Logical processors: {0}", item["NumberOfLogicalProcessors"]); }varNumberOfCores =0;foreach(varIteminch NewSystem.Management.ManagementObjectSearcher ("SELECT * from Win32_Processor"). Get ()) {numberofcores + =int. Parse (item["NumberOfCores"]. ToString ()); Console.WriteLine ("bitness: {0}", item["Addresswidth"]); Console.WriteLine ("Architecture: {0}", Getarchitecturedetail (int. Parse (item["Architecture"]. ToString ())); } Console.WriteLine ("Number of cores: {0}", numberofcores); }Private Static string Getarchitecturedetail(intArchitecturenumber) {Switch(Architecturenumber) { Case 0:return "x86"; Case 1:return "MIPS"; Case 2:return "Alpha"; Case 3:return "PowerPC"; Case 6:return "itanium-based Systems"; Case 9:return "x64";default:return "Unkown"; } } }}
Here is the output of the above program.
You can also browse through the Win32_ComputerSystem win32_processor WMI class to get more detailed information.
To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp
Get computer hardware information in C #