Windows exposes a great deal of system information through WMI (Windows Management Instrumentation). The 70-536 exam measures your capabilities to query WMI, subscribe to events, etc. Not the names of different WMI types (although I have seen test questions about those, but they could be figured out by pure logic).
To access WMI, you’ll need to set a reference on the System.Management namespace, and include a using directive for it. Then the following steps:
- Define the management scope.
- Create a ManagementScope object, and use the following path: \localhostrootcimv2. Don’t ask why, just use it. Then call the connect method of the ManagementScope instance.
- Create a query. Use the ObjectQuery class ‘s constructor to define a SQL-like query.
- Create a ManagementObjectSearcher instance, and pass the scope and query instances to the constructor.
- Create a ManagementObjectCollection class and assign it to the ManagementObjectSearcher’s Get method.
- Iterate over the ManagementObjectCollection by retrieving its properties.
ManagementScope scope = new ManagementScope(@”\localhostrootcimv2”);
ObjectQuery query = new ObjectQuery(“SELECT * FROM Win32_LogicalDisk”);
ManagementObjectSearcher mso = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection coll = mso.Get();
foreach(ManagementObject obj in coll)
I hope you get it. You can subscribe to WMI events two ways, asynchronously and synchronously. Both approaches use the same classes, let’s see them:
- Create an instance of WqlEventQuery by passing the event class name, a timespan and a condition.
- Initialize a new ManagementEventWatcher and pass the query to it.
- Create a ManagementBaseObject and call the ManagementEventWatcher’s WaitForNextEvent method.
- Wait until the event triggers, then examine the properties of ManagementBaseObject.
When you are working with asynchronous methods, simply register a method (object sender, EventArrivedEventArgs e) to the EventArrived event of the ManagemnetEventWatcher, and call its Start method. Something like this:
ManagementEventWatcehr mwe = new ManagementEventWatcher(myWqlQuery);
mwe.EventArrived += new EventArrivedEventHandler(mwe_EventArrived);
EventArrivedEventArgs define a ManagementBaseObject through the NewEvent property.