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:
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:
using WMI_ProcessorInformation;
You may now call one of the static methods like this:
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:
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.