Home > Uncategorized > How to find the Number of Cores and Logical Processors using C#.net

How to find the Number of Cores and Logical Processors using C#.net

January 30th, 2012

This is a pre cusrsor to something more that we are trying to achieve. The goal is to develop a simple code that will find the processor and memory details and then will benchmark the computer. We will then let users run this program to help him benchmark and compare the processors.

Today we will show you how to get the information about the number of processors. Here is the code that has been compiled in C#.Net ( 2010 Express Edition).

A note on compilation.

- Copy paste this code in console application and then go to Projects -> Add reference - > .NET - > System.Management

Happy coding.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Physical Processors: {0} ", item["NumberOfProcessors"]);
}

// Number of Cores

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);

// Number of Logical Processors

foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get())
{
Console.WriteLine("Number Of Logical Processors: {0}", item["NumberOfLogicalProcessors"]);
}

Console.ReadLine();
}
}
}

Uncategorized

  1. No comments yet.
  1. No trackbacks yet.