5,696,576 members and growing! (19,929 online)
Email Password   helpLost your password?
General Reading » Hardware & System » WMI     Intermediate License: The Code Project Open License (CPOL)

Using WMI to retrieve processor information in C#

By radialronnie

This article demonstrates how to use WMI (Windows Management Instrumentation) in C#, to retrieve several bits of information about the processor.
C#, Windows, Dev

Posted: 23 May 2008
Updated: 23 May 2008
Views: 5,074
Bookmarked: 7 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 1.27 Rating: 1.82 out of 5
2 votes, 40.0%
1
2 votes, 40.0%
2
0 votes, 0.0%
3
1 vote, 20.0%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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, Manufacturer, and other properties. Included in the first .zip is the executable 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 intimidates the somewhat more advanced ones. On the MSDN forums there are several questions on how to get CPU/Harddrive information. In this article I will demonstrate how to get a handful of CPU related properties, beginning on what hopefully will become a series of WMI articles/wrappers in the near future.

Using the Code

To use the wrapper, download the .cs file and place it in your application's solution folder: WMI_ProcessorInformationWrapper.zip - 901 B.

Next add a using reference to the namespace:

using WMI_ProcessorInformation;

the last step is to call one of the static methods like this:

WMI_Processor_Information.GetCpuManufacturer() ;

That's really all that there is to using the wrapper, so now lets take a tiny peek at some of what goes on behind the scenes. Most of the methods look like this one:

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 conveinient way of getting CPU information through the wrapper. To learn more about WMI check out some the great article here on codeproject: CodeProject Search for WMI

Future Wrappers

WMI is very powerfull and can be used to get the properties of many different system components such as:

Harddrive (total space, space remaining)
Battery (on a laptop)
Screensavers (executable path, timeout )
Wallpaper (image display mode, path to image)
hardware refridgeration!(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 drop a comment at 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 critisism and 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)

About the Author

radialronnie


My name is Daniel and I am 15 years old. I am very interested in C# application development. I am fairly proficiant in C# and am studying for my MCTS certification.
In my spare time I enjoy hiking in the Rocky Mountains, Nature Photography, and 3D modeling.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular Hardware & System articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralQuestion?memberUser of Users Group13:30 23 May '08  
GeneralRe: Question? [modified]memberradialronnie13:36 23 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 May 2008
Editor:
Copyright 2008 by radialronnie
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project