|
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.
Leave a Reply
Get Updates By Email
Popular Post
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

