|
Get Processor Information In .NET Using C#
.NET Framework provides classes which give information about the machine on which code is executed. One Such class is System.Environment which gives us things like Machine Name, OS Version and much more. I recently had a requirement to programmatically detect the number of Processors on a machine. I thought this was a simple enough task. System.Environment.ProcessorCount gives me that information. But I was in for a surprise. Let me walk you through my findings.
On my machine I have a dual core CPU which I can quickly see by opening Task Manager.

What do you think is the output of this statement.
Console.WriteLine(System.Environment.ProcessorCount.ToString());
The output is 2. But I have one physical processor so I expected to see 1. I could not find a way to get this information from the framework. So I went to good old WMI for this. Running this code gives me accurate information about my processor.
ManagementObjectSearcher mgmtObjects =
new ManagementObjectSearcher("Select * from Win32_ComputerSystem"); foreach (var item in mgmtObjects.Get()) { Console.WriteLine("Number Of Processors - " +
item["NumberOfProcessors"]); Console.WriteLine("Number Of Logical Processors - " +
item["NumberOfLogicalProcessors"]);
}
And the output this time is
Number Of Processors – 1
Number Of Logical Processors – 2
This is exactly what I need.
So here is my lesson learned. To get detailed and accurate information about number of processors on your machine, WMI is a better approach than System.Environment.ProcessorCount.
Get Updates By Email
Popular Post
- LINQ To SQL Tutorial
- LINQ To SQL Join On Multiple Conditions
- Code Sample: Programmatically Download File Using C#
- Free Icons And Images With Visual Studio 2008
- Windows 7 Control Panel In Classic Mode
- Dynamic Sort With LINQ
- Use SqlConnection With LINQ To SQL
- StyleCop Tutorial
- Write To Vista Event Log Using C#
- More Details Emerge On Microsoft Master Certification
Tag Cloud
Code Snippets
- Get Current Windows User In C#
- Get Width And Height Of Image In C#
- Get Windows Registry Size With WMI And C#
- Reverse Array Elements Using C#
- Convert Hexadecimal To Number In C#
- Get Free Disk Space Using T-SQL
- SQL Server 2008 – Get All Indexes In A Database
- Get Name Of Current Executing Assembly In C#
- Get CD Or DVD Drive Information Using WMI And C#
- Get Last Row From Table Using LINQ To SQL


March 6th, 2010 at 2:16 am
Sweet…. This is what I’m looking for
September 1st, 2010 at 1:47 am
This doesn’t work for me.
I added ‘using System.Management;’
But i keep getting the following error:
The type or namespace name ‘ManagementObjectSearcher’ could not be found
September 1st, 2010 at 9:23 am
Jerodev,
Looks like you are missing a reference to System.Management.dll