static void Main(string[] args)
{
List<string> Sockets=new List<string>();
int PhysicalCPU=0;
int LogicalCPU=0;
//Create WMI Class
ManagementClass mc = new ManagementClass("Win32_Processor");
//Populate class with Processor objects
ManagementObjectCollection moc = mc.GetInstances();
//Iterate through logical processors
foreach (ManagementObject mo in moc)
{
LogicalCPU++;
string SocketDesignation = mo.Properties["SocketDesignation"].Value.ToString();
//We will count the unique SocketDesignations to find
//the number of physical CPUs in the system.
if(!Sockets.Contains(SocketDesignation))
{
Sockets.Add(SocketDesignation);
}
}
PhysicalCPU = Sockets.Count;
Console.WriteLine(LogicalCPU + " logical CPUs detected.");
Console.WriteLine(PhysicalCPU + " physical CPUs detected.");
//Are there more logical than physical cpus?
//If so, obviously we are hyperthreading.
if (LogicalCPU > PhysicalCPU)
{
Console.WriteLine("HyperThreading is enabled.");
}
Console.ReadKey();
}
The sample code and available project are .net 2.0. If you want to use this in VS.net 2003, and .net 1.1, you will simply need to change our the List generic for an arraylist.