Disk Drive Dangers - SMART and WMI
Written by Mike James   
Wednesday, 08 August 2012
Article Index
Disk Drive Dangers - SMART and WMI
A practical project
Get Smart
Decoding WMI data


The key to using WMI  from .NET is the


class and the best way to find out how it works is via a simple example.

It is worth saying at this point that none of the following will work if the application isn't running with administrative privileges. While you are debugging under say Windows 7 this is most easily achieved by running Visual Studio as Administrator - right click and select the option from the context menu.




Start a new Windows project in C# Express or Visual Studio (running as administrator) and load a reference to System.Management – Project,Add Reference – and also add:

using System.Management;

to the start of the program.




Place a button on the form and enter all of the following code into its click event handler.

First we need a ManagementObjectSearcher object:

ManagementObjectSearcher WMISearch =
new ManagementObjectSearcher();

This will return a collection of WMI objects that match a search criterion specified as an ObjectQuery in its Query property. For example:

WMISearch.Query= new ObjectQuery(
"Select * from Win32_DiskDrive");

If you know any SQL you will see the similarity between WMI Query Language, for WSQ is indeed based on SQL.

The “*” is a “match all” condition and so all of the instances of the Win32_DiskDrive object are returned.

To actually retrieve the objects that match we use the Get method:

ManagementObjectCollection Drives =

The returned collection is always the same type irrespective of the actual type of the objects returned.

Now we can step through the drives collection and display whatever information interests us:

foreach ( ManagementObject Drive 
in Drives )

Notice the way that we have to retrieve the properties of the Drive object using a lookup in an array. This is the only easy way to deal with the fact that each WMI object is different and has its own set of properties and methods.

In this case the name of each drive in the system is listed in a MessageBox:




An alternative way of dealing with this difficulty is to use the WMI Extension for Server Explorer which is included with the full Visual Studio.

This automatically generates classes for each of the supported WMI objects, complete with suitable properties and methods. As not all WMI objects are supported, and the more basic method of accessing WMI is closer to the way scripts use WMI, this is the approach we will use – but if you are using a full copy of Visual Studio by all means use the automatically generated “wrapper” classes.

So to summarize, using WMI from .NET is a matter of:

  1. Creating an instance of ManagementObjectSearcher
  2. Working out the appropriate query and using it
  3. Using properties and methods of the objects returned

This is fairly simple and in practice most of the difficulty in using WMI is finding out what the classes are, what properties and methods they support, and determining if they actually work as advertised.

It is also worth knowing that you can make a WQL query in one step using an alternative ManagementObjectSearcher constructor:

ManagementObjectSearcher WMISearch =
new ManagementObjectSearcher(
"Select * from Win32_DiskDrive");



Last Updated ( Wednesday, 08 August 2012 )

RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.