Use C # To obtain computer hardware information
Now I will explain in the future How to Use WMI (Windows Management Instrumentation) in C # To obtain computer hardware information.
The purpose is to use WMI api to obtain the following information about the computer in C:
Number of physical Processors
Number of logical processors
Number of digits
System Architecture
Number of kernels
Create a console application in Visual Studio, right-click the reference, select "add reference", and then select "System. Management ".
Now the using statement includes System. Management. You can use WMI class reference in your code.
Here is the complete code for generating hardware information.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication8{ class Program { static void Main(string[] args) { GetCpuDetails(); } private static void GetCpuDetails() { foreach (var item in new System.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"]); } var numberOfCores = 0; foreach (var item in new System.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(int architectureNumber) { 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 the Win32_Processor WMI class through Win32_ComputerSystem for more details.