Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#

Using WMI to retrieve processor information in C#

Rate me:
Please Sign up or sign in to vote.
3.54/5 (18 votes)
9 Jun 2010CPOL2 min read 74.6K   3.2K   29   8
This article demonstrates how to use WMI (Windows Management Instrumentation) in C#, to retrieve several bits of information about the processor.

Introduction

This article demonstrates how to use WMI (Windows Management Instrumentation) in C#, to retrieve several types of information about the processor, such as the CPU clock speed, voltage, and manufacturer. Included in the first .zip file is the executable that polls your system for all the implemented properties. The bench-marker looks like this:

wmi_processorinfo_capture.jpg

Background

It seems that WMI is an unknown concept to many beginners and maybe it even intimidates the somewhat more advanced ones. On the MSDN forums there are several questions on how to get CPU/Hard-drive information. In this article I will demonstrate how to get a handful of CPU related properties, beginning what hopefully will become a series of WMI articles/wrappers in the near future.

Using the Code

To use the wrapper, download and unzip the file wmi_processorinformationwrapper.zip. Place the .cs file in your application's solution folder. Next add a using reference to the namespace:

C#
using WMI_ProcessorInformation;
You may now call one of the static methods like this:
C#
WMI_Processor_Information.GetCpuManufacturer();

That's really all that there is to using it

Behind the Scenes

Lets take a tiny peek at some of what goes on behind the scenes. Most of the methods look similar to this:

C#
public static string GetCpuManufacturer()
{
    try
    {
        ManagementObjectSearcher searcher = 
            new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");
        foreach (ManagementObject queryObj in searcher.Get())
        {
            return queryObj["Manufacturer"].ToString();
        }
    }
    catch (Exception e)
    {
        return null;
    }
    return null;
}

This article was not meant to teach WMI but rather to present a convenient way of getting CPU information using the wrapper. To learn more about WMI, check out some the great articles here on Code Project: Code Project Search for WMI.

Future Wrappers

WMI is very powerful and can be used to get the properties of many different system components including:

  • Hard-drive (total space, space remaining)
  • Battery (on a laptop)
  • Screen-savers (executable path, timeout )
  • Wallpaper (image display mode, path to image)
  • Hardware refrigeration (fan RPM,etc),
  • User Account (User name, User's time zone)

Do you have any specific WMI wrapper you would like to see for C#? If so just add a comment to this article and I'll try to get it written!

P.S. As this is my first article please cut me at least a little slack, but I am NOT opposed to constructive criticism. We'll have an article version at 2.5 before you know it.

History

V 0.9 Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMessage Removed Pin
23-Oct-12 8:25
Christopher Smit23-Oct-12 8:25 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey25-Apr-12 0:27
professionalManoj Kumar Choubey25-Apr-12 0:27 
GeneralWin32_VoltageProbe using C#.. Pin
tallerguy23-Mar-11 7:01
tallerguy23-Mar-11 7:01 
Questionwhy not use mgmtclassgen.exe ? Pin
sdahlbac10-Jun-10 0:10
sdahlbac10-Jun-10 0:10 
Generalproblem to get software features installed on the pc with ProSysLib. Pin
lwiji-tn26-Feb-09 0:04
lwiji-tn26-Feb-09 0:04 
GeneralRe: problem to get software features installed on the pc with ProSysLib. Pin
radialronnie26-Feb-09 12:34
radialronnie26-Feb-09 12:34 
GeneralRe: problem to get software features installed on the pc with ProSysLib. Pin
lwiji-tn3-Mar-09 23:58
lwiji-tn3-Mar-09 23:58 
QuestionQuestion? Pin
User of Users Group23-May-08 12:30
User of Users Group23-May-08 12:30 
The article could use a bit of expansion for better votes I guess, I didn't vote on it, but hey I am not interested in that Smile | :) Smile | :)


I really wonder how they pulled it off in WMI when one of the problems was obtaining the correct number of cores, physical or logical whatever it was processors via the Win32 API.

OMG | :OMG: WTF | :WTF: OMG | :OMG: Confused | :confused:

And they only fixed it on x64 and Vista..

Any news on that? aka will this really work aka does anyone know if that brilliant innovation in API testing has been patched up ?
AnswerRe: Question? [modified] Pin
radialronnie23-May-08 12:36
radialronnie23-May-08 12:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.