Home > all > Using Windows Management Instrumentation(WMI) to get Hardware Information in .Net

Using Windows Management Instrumentation(WMI) to get Hardware Information in .Net

I’m currently working on some new projects in .Net which use the new addition for Parallel Programming in the .NET Framework. While developing I noticed that my simple-log really isn’t that great in a Multithreaded Project. I was using a simple text file. I’m now switching to an new log-system, I’m currently working on, but this is a different post.

As I was designing the new log-system I saw that I don’t had any information about the system the application was running on. I was dependent that everybody who send me a log for a bug report also tells me their system information. This wasn’t that clever. So I started to look for a way to get all those data in .Net and I found out that WMI also can be easily be accessed from .Net.

The code to access WMI is in the System.Management namespace. This is in the Assembly with the same name. Which is referenced by default, so don’t forget to add the reference.

When you have found all the different pieces needed to query all the different data you have done the task which takes most of the time. Sadly I haven’t found any resource which had all the links in one place. I will post the links to the documentations you need at the end of the post.

When we have all the documentation open it’s really easy to get a lot of information from the hardware. So lets start.

The first step is to create a ManagementObjectSearcher. A ManagementObjectSearcher returns a collection of ManagementObject based on the query defined when creating the Searcher. The ManagementObjectSearcher overload I will be using here takes two strings. The first argument contains the scope in which the query should be executed and the second argument is the actual query. But lets look at the code:

ManagementObjectSearcher searcher = new ManagementObjectSearcher(“root\\CIMV2″, “SELECT * FROM Win32_Processor”);

This ManagementObjectSearcher queries all the Processors in the “root\\CIMV2” scope, this is the default scope, it contains all the hardware.

Now we have to get the actual processors from the searcher.
foreach (ManagementObject wmiObject in searcher.Get())
{
// [Add Code here]
}
The searcher.Get() methods returns a ManagementObjectCollection so we use a foreach to get the data. Maybe the PC has multiple elements of this kind as well so keep in mind to handle those cases as well.

Now it’s time again to look in the documentation, in this case we have to look at the WIN_32_Processor documentation. At this page you get a look at all the different properties you have access to.

If you have found the name of the property you want to get the data from you just have to call the following code. You just have to replace the string with the name the property you want to get the value from.
object clockSpeedObject = wmiObject.GetPropertyValue(“CurrentClockSpeed”);
uint clockSpeed = (uint) clockSpeedObject;
GetPropertyValue(..) will always return a object so you have to cast it your self, always check the types the Property values is in.

This is everything needed you need to know. Below are some samples showing how to retrieve data from GPU, CPU and RAM.

Hope you find this useful. This article just scratches the surface of what WMI is able to do.

GPU

ManagementObjectSearcher searcher = newManagementObjectSearcher(“root\\CIMV2″, “SELECT * FROM Win32_VideoController”);

foreach (ManagementObject wmiObject in searcher.Get())
{
object memoryInBytes = wmiObject.GetPropertyValue(“AdapterRAM”);

// some video controllers are listed without RAM
// those aren’t real cards, so can we ignore them
if (memoryInBytes != null)
{
// calculate Memory in MB
uint memoryInMB = (uint)memoryInBytes / 1024 / 1024;

string cardName = (string)wmiObject.GetPropertyValue(“Description”);
string driverVersion = (string)wmiObject.GetPropertyValue(“DriverVersion”);
string manufacturer = (string)wmiObject.GetPropertyValue(“AdapterCompatibility”);
}
}

CPU

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
 "root\\CIMV2", "SELECT * FROM Win32_Processor");

foreach (ManagementObject wmiObject in searcher.Get())
{
      object clockSpeedObject = wmiObject.GetPropertyValue("CurrentClockSpeed");

      // get all the informations about the processor
      uint clockSpeed = (uint)wmiObject.GetPropertyValue("CurrentClockSpeed");
      uint physicalCoreCount = (uint)wmiObject.GetPropertyValue("NumberOfCores";
      uint logicalCoreCount = (uint)wmiObject.GetPropertyValue("NumberOfLogicalProcessors");
      string manufacturer = (string)wmiObject.GetPropertyValue("Manufacturer");
      string name = (string)wmiObject.GetPropertyValue("Name");
}

RAM

ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                     "root\\CIMV2", "SELECT * FROM Win32_PhysicalMemory");

int totalMemory = 0;

// calculate the RAM capacity we have
//  we have to add every RAM-Stick Capacity to get the fincal value
foreach (ManagementObject wmiObject in searcher.Get())
{
     ulong capacityInBytes = (ulong)wmiObject.GetPropertyValue("Capacity");
     // calculate in MB
     ulong capacityInMB = capacityInBytes / 1024 / 1024;
     totalMemory += (int)capacityInMB;
}

Links


Categories: all
  1. No comments yet.
  1. No trackbacks yet.