Click here to Skip to main content
Click here to Skip to main content

Windows Management Instrumentation (WMI) made easy

, 14 Oct 2003
Rate this:
Please Sign up or sign in to vote.
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.

using System.Management;
using EasyWMI;

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

//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.

//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.

//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

Share

About the Author

Holger Jannasch
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionTest connection PinmemberRadu_2021-Nov-07 6:43 
GeneralThanks PinmemberVenilla8-Feb-07 23:38 
GeneralA good beginning PinmemberMichael P Butler19-Oct-03 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 15 Oct 2003
Article Copyright 2003 by Holger Jannasch
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid