Home > Article > Backend Development > How to find the number of CPU cores in C#?
We can get a lot of different information related to the processor
These can all be different; take a machine with 2 dual-core hyperthreading enabled as an example Processors, with 2 physical processors, 4 cores, and 8 logical processors.
The number of logical processors can be obtained through the Environment class, but Additional information is only available through WMI (and you may need to install some On some systems, a hotfix or service pack needs to be installed for operation) −
Add a reference to System.Management.dll in your project. In .NET Core, this is provided as a NuGet package (Windows only).
class Program{ public static void Main(){ foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()){ Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]); } Console.ReadLine(); } }
Number Of Physical Processors: 1
class Program{ public static void Main(){ int coreCount = 0; foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get()){ coreCount += int.Parse(item["NumberOfCores"].ToString()); } Console.WriteLine("Number Of Cores: {0}", coreCount); Console.ReadLine(); } }
Number Of Cores: 2
class Program{ public static void Main(){ Console.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount); Console.ReadLine(); } }
Number Of Logical Processors: 4
The above is the detailed content of How to find the number of CPU cores in C#?. For more information, please follow other related articles on the PHP Chinese website!