Click here to Skip to main content
15,434,755 members
Articles / Desktop Programming / Win32
Posted 8 Dec 2007


115 bookmarked

WMI Interface for .NET

Rate me:
Please Sign up or sign in to vote.
4.98/5 (45 votes)
9 Dec 2007CPOL3 min read
A class library to aid in writing WMI applications.
Screenshot - EasyWMIDemo - click to enlarge


I've been writing small WMI applications off and on for the past few years and have always found that writing such an application in .NET has always seemed somewhat cumbersome. I couldn't help but think there was a better way to write a WMI application in .NET. So I started out to search the Internet for anything I could find that might help speed up the development process. Everywhere I turned I found article after article showing people how to use the System.Management namespace within .NET to accomplish their goals. Some of these writings were very good, and some could probably use a little more work. But one thing they always had in common, were the strings of comments after the article asking “How do I do ‘this’…” or “Is it possible to do ‘this’…”. Which led me to think, did this article really answer the questions people are having? The answer I found to my own question was, yes, and no.

One of the problems with writing a WMI application in .NET is that there isn't a direct way to work with the properties and methods of the built-in classes. Every solution ends with developers referencing properties via a string index within a ManagementObject object.

ManagementObject oMngObject = MyObejctCollection[0];
lblDemo.Text = oMngObject["Manufacturer"];

This being a completely valid method for working with WMI properties, it doesn't do much good if you don't know the name of the property you're trying to access. This is where EasyWMI comes in.

Breakdown of EasyWMI

I developed the EasyWMI class library to help speed up the development process of developing WMI driven applications. It includes a large percentage of all of the Win32 classes within the root/cimv2 namespace. The library consists of two main parts: controllers and models. The models are the Win32 classes pulled directly from WMI within Windows and contain all the properties and methods for each. The controllers are what create and populate all of the models for each class. For every model there is a corresponding controller that accompanies it. The naming scheme for the controllers is the same naming scheme as the models without the preceding Win32_ prefix. For example, the Win32_Process class has a corresponding controller named Process. One thing to note when using the source of EasyWMI, is that I did not physically write all of the models and controllers. I actually threw together a code generator using CodeDOM specifically for this purpose. So when working with the actual source, be warned, indentations may be a little off, making it a little awkward to read, but not difficult.

Using the Code

To use EasyWMI is very simple. Create a new instance of the class controller you wish to use and run the SelectAll() method. Each controller contains 5 overloaded constructors; the default constructor assumes “localhost”.

// Create a new instance of ComputerSystem.
ComputerSystem CS = new ComputerSystem();
IList<win32_computersystem /> CSCollection = CS.SelectAll();
Win32_ComputerSystem MyCS = CSCollection[0];

SelectAll() returns a generic IList<t /> collection of type Win32_CLASSNAME. Some classes will only return one item within the collection, such as ComputerSystem and OperatingSystem, for example. You now have access to all of the methods and properties for that particular object of the Win32_ComputerSystem type. For classes that return more than one object, you can simply use a foreach loop to iterate through the collection.

static void DisplayLogicalDisks(IList<win32_logicaldisk /> Drives)
      SetupSection("Logical Disks");
            foreach (Win32_LogicalDisk LogicalDisk in Drives)
                  Console.WriteLine("Drive " + LogicalDisk.DeviceID);
                  Console.WriteLine("    Volume:\t\t" + LogicalDisk.VolumeName); 
                  Console.WriteLine("    Size:\t\t" + ConvertToMB(LogicalDisk.Size));
                  Console.WriteLine("    Free Space:\t\t" + ConvertToMB
                  Console.WriteLine("    File System:\t" + LogicalDisk.FileSystem);
                  Console.WriteLine("    Caption:\t\t" + LogicalDisk.Caption);
                  Console.WriteLine("    Serial:\t\t" + LogicalDisk.VolumeSerialNumber);
      catch (Exception ex)
            Console.WriteLine("Error retrieving information.");
            Console.WriteLine("Message: " + ex.Message);

Now you have an idea of how simple it is to access properties for each of the Win32 classes, but what about using their methods? Simple, use them the way you would use any method.

string Domain;
string User;
Process SysProcess = new Process();
IList<win32_process /> Processes = SysProcess.SelectAll();

foreach (Win32_Process Process in Processes)
      if (Process.Name == "iexplore.exe")
            Process.GetOwner(out Domain, out Username);

You can also specify a different query to use instead of the default select * from.

CS = new ComputerSystem(ComputerName);
CS.EasyWMICfgUsername = @"yourdomain\username";
CS.EasyWMICfgPassword = "password";
CS.EasyWMICfgQuery = "select Manufacturer from";
CSCollection = CS.SelectAll();

You may notice I call the SetScope() method after I set some configuration options. You'll need to run the SetScope() method anytime you change any of the configuration options before you run the SelectAll() method.


Although this may not be the most complete and yet still not the best way to interface with WMI using .NET, I hope this helps simplify working with WMI and speed up your development process.


No updates as of yet.


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

Written By
Software Developer (Senior)
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

QuestionVote 5 and one Question Pin
Dean Feng11-Feb-17 5:00
professionalDean Feng11-Feb-17 5:00 
QuestionCan you please make a demo of this as a non console application? Pin
TeckyBoy5-Mar-13 9:53
MemberTeckyBoy5-Mar-13 9:53 
BugNot working on static member Win32_NetworkAdapterConfiguration Pin
dimox543-Jan-13 12:49
Memberdimox543-Jan-13 12:49 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey25-Apr-12 0:29
professionalManoj Kumar Choubey25-Apr-12 0:29 
GeneralMy vote of 5 Pin
s0nyguy13-Apr-12 8:51
Members0nyguy13-Apr-12 8:51 
GeneralMy vote of 5 Pin
Sergey Lapp5-Jul-11 20:40
MemberSergey Lapp5-Jul-11 20:40 
GeneralMy vote of 5 Pin
paritosh_sumrao17-Apr-11 8:44
Memberparitosh_sumrao17-Apr-11 8:44 
QuestionQuestion on WMI Pin
Igor Ladnik6-Apr-11 1:51
professionalIgor Ladnik6-Apr-11 1:51 
AnswerRe: Question on WMI Pin
jsunstrom6-Apr-11 3:42
Memberjsunstrom6-Apr-11 3:42 
GeneralRe: Question on WMI Pin
Igor Ladnik6-Apr-11 6:07
professionalIgor Ladnik6-Apr-11 6:07 
GeneralFantastic piece of work Pin
scott.leckie18-Aug-09 12:07
Memberscott.leckie18-Aug-09 12:07 
GeneralRe: Fantastic piece of work Pin
jsunstrom30-Oct-09 8:26
Memberjsunstrom30-Oct-09 8:26 
QuestionHow to use this WMI interface to scan WiFi signal strength ? Pin
wangyongcai9-Mar-09 22:42
Memberwangyongcai9-Mar-09 22:42 
AnswerRe: How to use this WMI interface to scan WiFi signal strength ? Pin
jsunstrom10-Mar-09 9:30
Memberjsunstrom10-Mar-09 9:30 
GeneralWMI query doesnot return record for user having less previleges!! Pin
Cracked-Down2-Mar-09 19:51
MemberCracked-Down2-Mar-09 19:51 
GeneralRe: WMI query doesnot return record for user having less previleges!! Pin
jsunstrom3-Mar-09 4:00
Memberjsunstrom3-Mar-09 4:00 
AnswerRe: WMI query doesnot return record for user having less previleges!! Pin
Cracked-Down4-Mar-09 2:52
MemberCracked-Down4-Mar-09 2:52 
QuestionRe: WMI query doesnot return record for user having less previleges!! Pin
miesch111-May-09 3:33
Membermiesch111-May-09 3:33 
GeneralVery slow; shouldn't use reflection Pin
Luther Bruck22-Jan-09 4:14
MemberLuther Bruck22-Jan-09 4:14 
GeneralRe: Very slow; shouldn't use reflection Pin
jsunstrom30-Oct-09 8:48
Memberjsunstrom30-Oct-09 8:48 
QuestionAbout Exceptions Pin
Member 354156214-May-08 22:47
MemberMember 354156214-May-08 22:47 
QuestionNot usable from WPF apps ? Pin
Philippe Chessa30-Jan-08 5:49
professionalPhilippe Chessa30-Jan-08 5:49 
AnswerRe: Not usable from WPF apps ? Pin
jsunstrom30-Jan-08 5:52
Memberjsunstrom30-Jan-08 5:52 
GeneralRe: Not usable from WPF apps ? Pin
Philippe Chessa30-Jan-08 6:14
professionalPhilippe Chessa30-Jan-08 6:14 
GeneralRe: Not usable from WPF apps ? Pin
Philippe Chessa30-Jan-08 6:39
professionalPhilippe Chessa30-Jan-08 6:39 

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.