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.
ConnectionOptions oConn = new ConnectionOptions();
CWMIManager wmi = new
CWMIManager("localhost", string.Empty, oConn);
With the instance of the CWMIManager
class, you can now directly submit WQL-queries.
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.
CWMIDriveManager drive = new CWMIDriveManager(wmi);
Console.WriteLine("*******************************************");
ManagementObjectCollection oDrives = drive.getDrives();
foreach(ManagementObject oDrive in oDrives)
{
Console.WriteLine(Convert.ToString(oDrive["Caption"]));
}
History
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.