有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量、核心数量和逻辑处理器数量,这些可以不同。两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器、4个核心和8个逻辑处理器。
逻辑处理器数是可通过Environment
类获取,但其他信息都是只可通过WMI(您可能需要安装一些修补程序或服务包)获取:
物理处理器:
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
}
内核:
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.WriteLine("Number Of Logical Processors: {0}", Environment.ProcessorCount);
或
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
}
原文地址:http://www.itstrike.cn/Question/how-to-find-the-number-of-cpu-cores-via-net-csharp