|
The C# Column
Getting management information using WMI
As
enterprise systems, applications, and networks became larger and
more complex, the challenge in front of the managers to manage them
also increased. In order to solve these problems, Microsoft developed
WMI, a scalable management infrastructure and shipped it with Windows
2000. When we implement WMI on Windows platforms, the technology
enables the Common Information Model (CIM) designed by the Distributed
Management Task Force (DMTF) to represent systems, applications,
networks, and other managed components.
The CIM Specification
describes the modeling language, naming, and mapping techniques used to collect
and transfer information from data source and other management models.
The CIM schema provides the actual model descriptions and information framework.
It defines a set of classes with properties and associations, making it possible
to organise information about the managed environment.
The key features of WMI are discussed below:
- Uniform Scripting API: WMI uses the CIM object model to create the managed
objects. So, scripts only need to use a single API of WMI to access information
for numerous disparate sources.
- Remote Accessibility: Objects managed within WMI can be used by the applications
and scripts running on the local machine as well as on the remote machine.
- Discovery: Applications and scripts are able to discover what information
is available about a system by enumerating the classes that are available.
- WMI Queries: WMI allows SQL queries to be executed on its objects.
- Powerful Event Publication: Events can be requested for any change in the
managed objects in the system, regardless of whether they support an internal
event capability.
If you have used WMI in the past, you might be daunted by its complexities as
it exposes COM interfaces. You will be happier to know that .NET provides easy-to-use
namespaces and classes that encapsulate the WMI API. We will start with a simple
program that displays information of the drive entered by the user.
Create a Windows Forms application and design the form as shown.
We have added
few labels and read-only text boxes to the form. Give suitable Names to the
text boxes. The user enters the drive name, for example, C: in the text box
named Enter Drive Name. To get the information of the specified
drive the user clicks the Get Info button.
Add the Click event handler for the button. This handler is given below:
private void binfo_Click ( object sender, System.EventArgs
e )
{
ManagementClass
mc = new ManagementClass
( Win32_LogicalDisk
) ;
ManagementObjectCollection
disks = mc.GetInstances( ) ;
foreach
( ManagementObject disk in disks )
{
if ( disk [ Name ].ToString( ) == tdrive.Text )
{
text1.Text = disk [ DeviceID ].ToString( ) ;
text2.Text = disk [ DriveType ].ToString( ) ;
text3.Text = disk [ FileSystem ].ToString( ) ;
text4.Text = disk [ FreeSpace ].ToString( ) ;
text5.Text = disk [ MediaType ].ToString( ) ;
text6.Text = disk [ Size ].ToString( ) ;
text7.Text = disk [ VolumeName ].ToString( ) ;
text8.Text = disk [ VolumeSerialNumber ].ToString( ) ;
}
}
}
Here, we have firstly declared a reference mc to the ManagementClass object.
To the constructor of this class we have passed the name of the management class
Win32_LogicalDisk. The Win32_LogicalDisk WMI class represents a data source
that resolves to an actual local storage device on a Windows system. The idea
is to obtain the objects of the logical disk class at run time and use its properties
to obtain the management information.
Then we have used the GetInstances( ) method to retrieve all the instances of
the logical disk class. It returns the reference to the collection of the class
instances.
We have then applied the foreach loop to enumerate all the instances. The object
of the ManagementObject class represents a management data object. Using the
Name property of the logical disk class we have retrieved the name of the drive.
If this name matches the name of the drive entered by the user, then we have
obtained the information of the drive using various properties of the Win32_LogicalDisk
class. We have displayed this information in the text boxes.
Declare the System.Management namespace at the beginning of the program. Also
add the reference to System.Management.dll through the Solution
Explorer window.
Run the program. Enter the drive name as C: and click the GetInfo
button. The drive information gets displayed, as shown in the following snapshot.
What we saw in this article is only the tip of the iceberg.
In the next few articles we will study the WMI architecture and
use more WMI classes to explore the management information.
 |
Yashavant Kanetkar, one of the first
Express Computer columnists, is an established software expert,
speaker and author with several best-sellers to his credit,
including titles like “Let Us C” and the “Fundas” series. Contact
him at kanetkar@dcubesoft.com |
|