WMI Programming with Visual Basic.NET: Breaking the Ice - Getting information from WMI
(Page 3 of 4 )
In order to get information (or data) from WMI, either on the local computer or from a remote computer, you must connect to the WMI service by connecting to a specific namespace. For example, the "root\cimv2" namespace (which is also the default) includes most of the classes that represent resources commonly associated with a computer and operating system. "root" is the main namespace to start with WMI. "cimv2" is a sub namespace available within the "root". There exist literally hundreds (or even thousands) of classes in the "root\cmv2" namespace. Similarly, there exist several other namespaces as well, logically grouped based on specific tasks.
The above paragraph provides you with just a procedure for working with WMI. But how do you implement it? The version of WMI that shipped with Windows 2000/2003 displayed classic COM interfaces to client applications. Therefore, you can access WMI-instrumented management data and events via the .NET COM Interop layer.
But there is a much better way to work with WMI from .NET. The System.Management namespace, defined as part of the .NET Framework, provides an easy-to-use, intuitive API for consuming WMI data and events. A lot of the complexities and idiosyncrasies of WMI APIs are taken care of by System.Management, and its class definitions follow the standard design paradigm of the .NET Framework.
Now, I would like to take a moment to explain the program in the previous section. Let us consider the following statement.
Imports System.Management
The above statement straight away imports all the necessary ingredients to work with WMI in our application. It must be added as a reference to our application before importing it.
Dim diskClass As New ManagementClass("Win32_LogicalDisk")
The above statement connects to a WMI class called "Win32_LogicalDisk". WMI creates a separate instance of this class for each and every logical drive present in the system. The class exists in "root\cimv2" namespace, which is the default namespace. You need not specify "root\cimv2" namespace, if you are working with any class within that namespace. If you really would like to do so, you need to change the above statement to something like the following:
Dim diskClass As New ManagementClass ("\\.\root\cimv2:Win32_LogicalDisk")
It is something like the UNC (Universal Naming Convention). The following gives you syntax for using the convention:
\\<ServerName>\root\<subnamespace>:<class>
Here is the next statement in the program:
Dim disks As ManagementObjectCollection = diskClass.GetInstances()
The above statement returns all the "instances" (or drives) of drives logically present in your system. Each drive returned (as part of the collection) is an "instance" of the class "Win32_LogicalDisk".
For Each disk In disks
arDisks.Add(disk("deviceid").ToString())
Next disk
The above loop iterates through each instance of drives (present in the collection) and adds the "deviceid" to the arraylist. "deviceid" is a public member of the class "Win32_LogicalDisk". As we received the instances of the class "Win32_LogicalDisk" (from WMI), we are retrieving the value available in the "deviceid" member of each of those instances using a loop.
Me.lstDrives.Items.AddRange(arDisks.ToArray)
Finally, we add all the elements available in the arraylist to the listbox.
Next: Our first program to connect to WMI (on a remote computer) >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee