[ download code ]
I will discuss two radically different methods of interfacing with WMI instances both of which are fairly simple and straight forward. Basically it comes down to a personal choice and the requirements defined by the application that you are writing.
WMI Query Language (WQL) allows you to query WMI providers using a SQL-like query language, so if you are familiar with SQL then you will find WQL all to familiar. If you know the provider classes and the fields available, then you can get the info very easily. For instance, if you wanted to get a list of logical drives from a system you would use the following query:
Select * from Win32_LogicalDisk
You can, however, refine the search by using where clauses and getting specific "fields" in the query. The following query gets the amount of free space, the size, and the name of all fixed disk drives:
Select FreeSpace, Size, Name from Win32_LogicalDisk where DriveType = 3
If you are not sure what provider classes and fields are available to you then this is where the WMI Administrative Tools will come in handy. Take the following steps:
- Download and install the WMI Administrative Tools.
- Once installed under the WMI Tools program group, click "WMI Object Browser".
The object browser will allow you to view the provider classes and associated field within a given class. Upon running the object browser you will be presented a screen as follows:
- Click OK. Then you will be presented a screen similar to the following:
As you can see in the above instance we are looking at the Win32_ComputerSystem provider class on the left hand side and the available fields on the right hand side of the above screen. In order to perform WQL queries in you class you will need to import the System.Management Namespace. The classes we will discuss are: