Microsoft

Interact with the operating system via .NET's Windows Management Instrumentation

The .NET Framework's Windows Management Instrumentation (WMI) services allows you to interact and manipulate various aspects of the environment in which the application is running. Here's an overview of WMI's functionality.

The .NET Framework is no longer new, but it continues to delight me with the power it offers when developing solutions. A feature that has garnered my attention is WMI (Windows Management Instrumentation) services. Basically, it provides the vehicle for interacting with the underlying operating system (albeit Windows XP, Windows 2000, and so forth). In previous incarnations of Windows development, I was forced to go to the Windows API (Application Programming Interface) when such functionality was needed, so WMI is a blessing.

Overview of WMI

WMI provides management functionality. It is available on Windows XP and to a lesser degree on earlier Windows versions. The System.Environment.dll file provides the WMI functionality, so it must be added (as a reference) to a .NET application when it is used. It also utilizes COM to work with the underlying environment.

WMI providers wrap native Windows API code to be utilized via WMI. The providers are utilized by the WMI object manager. If you need to utilize the underlying native API, you work with the WMI object manager via the System.Environment namespace.

The .NET Framework includes a variety of WMI providers to perform common tasks. Here is a small sampling of these providers:

  • Active Directory Provider: allows access to the Active Directory. The first Windows platform to support it is 2000.
  • Configuration Provider: allows you to configure the .NET CLR (Common Language Runtime). This is included with the .NET Framework.
  • Disk Quota Provider: allows you to control disk quotas. The first Windows platform to support it is XP.
  • Event Log Provider: allows access to the Event Log. The first Windows platform to support it is NT.
  • Performance Counter Provider: allows access to performance counters. The first Windows platform to support it is 2000.
  • System Registry Provider: allows access to the Registry. The first Windows platform to support it is NT.
  • Win32 Provider: allows access to environment variables. The first Windows platform to support it is 2000.

You may also create custom providers. WMI has been included with the Windows operating system beginning with Windows ME, but the available providers depend upon the Windows version. Microsoft does offer the WMI administrative tools, which allow you to see what is available.

WMI in action

Now that you have a general idea of WMI and its functionality, let's examine taking advantage of it within your .NET code. As previously stated, you must add a reference to the System.Environment.dll to your project. In addition, you must include the System.Environment namespace in your code (via the Using statement in C#, import in J#, and Imports within VB.NET). The following example accesses miscellaneous system properties:

using System;
using System.Management;
namespace Builder {
class WMIExample {
[STAThread]
static void Main(string[] args) {
try {
ManagementObject mo = new
ManagementObject("Win32_ComputerSystem.Name=\"PENTIUM4\"");
Console.WriteLine("Description: " + mo["Description"].ToString());
Console.WriteLine("Model: " + mo["Model"].ToString());
Console.WriteLine("Number of processors: " +
mo["NumberOfProcessors"].ToString());
} catch (ManagementException e) {
Console.WriteLine("Exception encountered: " + e.ToString());
} catch (Exception e) {
Console.WriteLine("Exception encountered: " + e.ToString());
} } } }

Here's the VB.NET equivalent:

Imports System.Management
Module Module1
Sub Main()
Try
Dim mo As ManagementObject
mo = New ManagementObject("Win32_ComputerSystem.Name=""PENTIUM4""")
Console.WriteLine("Description: " + mo("Description").ToString())
Console.WriteLine("Model: " + mo("Model").ToString())
Console.WriteLine("Number of processors: " + mo("NumberOfProcessors").ToString())
Catch e As ManagementException
Console.WriteLine("Management exception encountered: " + e.ToString())
Catch e As Exception
Console.WriteLine("Exception encountered: " + e.ToString())
End Try
End Sub
End Module

Let's examine a few details of the code. First, an instance of the ManagementObject class is used to work with WMI. You can use a ManagementObject class to work with any object that's accessible via WMI. The WMI class (Win32_ComputerSystem.Name="Pentium") is used to instantiate the MangementObject object. You can use the previously mentioned WMI administrative tools (WMI Object Browser) to discover the WMI classes available on the target system. In addition to retrieving information, you may alter values.

The next code sample changes the name assigned to a hard drive:

using System;
using System.Management;
namespace Builder {
class WMIExample {
[STAThread]
static void Main(string[] args) {
try {
ManagementObject mo = new ManagementObject("Win32_LogicalDisk.DeviceID=\"F:\"");
mo.Get();
mo["VolumeName"] = "FDrive";
mo.Put();
} catch (ManagementException e) {
Console.WriteLine("Exception encountered: " + e.ToString());
} catch (Exception e) {
Console.WriteLine("Exception encountered: " + e.ToString());
} } } }

Here's the VB.NET equivalent:

Imports System.Management
Module Module1
Sub Main()
Try
Dim mo As ManagementObject
mo = New ManagementObject("Win32_LogicalDisk.DeviceID=""F:""")
mo.Get()
mo("VolumeName") = "New Volume"
mo.Put()
Catch e As ManagementException
Console.WriteLine("Management exception encountered: " + e.ToString())
Catch e As Exception
Console.WriteLine("Exception encountered: " + e.ToString())
End Try
End Sub
End Module

This example is similar to the first one; the difference is that the drive name property has changed. The get method is called before making the change; it ensures the WMI instance is correctly initialized with the current state of the drive. After the new name has been assigned, the put method is called to update the underlying object (note: be sure to save the change).

The functionality is available when needed

WMI is an important aspect of the .NET Framework that receives little attention. It allows you to interact and manipulate various aspects of the environment in which the application is running. In addition, you may use WMI to monitor certain aspects of the environment such as changes to the system via events.

TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET. Automatically sign up today!

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox