Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#
Article

Windows Management Instrumentation (WMI) made easy

Rate me:
Please Sign up or sign in to vote.
3.15/5 (12 votes)
14 Oct 2003 69.4K   705   29   3
Some easy to use classes to query WMI objects.

Sample Image - testrun.gif

Introduction

The .NET System.Management namespace is in most cases, not straight forward. If you want to make some quick success in calling WMI from local or remote machines, take a look at this small class library.

Background

To read something about the standards from the DMTF, look here. WMI is an implementation from Microsoft of the CIM standards from the DMTF, the documentation from Microsoft can be found here.

Using the code

If you want to use these classes in your own project, add a reference to the System.Management library and to the class library easywmi.

In your class, add these two using statements.

C#
using System.Management;
using EasyWMI;

Now you can connect to a WMI source, but first you have to set some connection options.

C#
//set connection options
//Connection credentials to the remote computer 
//- not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();
//oConn.Username = "test";
//oConn.Password = "test";    

//create new WMI manager
CWMIManager wmi = new 
  CWMIManager("localhost", string.Empty, oConn);

With the instance of the CWMIManager class, you can now directly submit WQL-queries.

C#
//execute WQL directly
//get system information
Console.WriteLine("*******************************************");
ManagementObjectCollection oCompSys = 
  wmi.ExecWQL("SELECT * FROM Win32_ComputerSystem");
foreach(ManagementObject oReturn in oCompSys) 
{
    Console.WriteLine(Convert.ToString(oReturn["Caption"]));
}

Or you can use a specific manager class from the library, when you don't know the WQL-statement.

C#
//use a manager
//create new drive manager
CWMIDriveManager drive = new CWMIDriveManager(wmi);

//get drives
Console.WriteLine("*******************************************");
ManagementObjectCollection oDrives = drive.getDrives();
foreach(ManagementObject oDrive in oDrives) 
{
    Console.WriteLine(Convert.ToString(oDrive["Caption"]));
}

History

  • Version 1.0 - 2003-10-15

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionTest connection Pin
Radu_2021-Nov-07 5:43
Radu_2021-Nov-07 5:43 
GeneralThanks Pin
Arun Varadharajan8-Feb-07 22:38
Arun Varadharajan8-Feb-07 22:38 
GeneralA good beginning Pin
Michael P Butler19-Oct-03 22:46
Michael P Butler19-Oct-03 22:46 

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.