Working with Windows Management Instrumentation

Thursday Jan 17th 2013 by Joydip Kanjilal

Windows Management Instrumentation (WMI) is a COM based technology used to get system-related information. Joydip Kanjilal shows you how to leverage the power of WMI to retrieve system information.

Windows Management Instrumentation (WMI) is a COM based technology used to get system-related information. Such information may include hardware resources like, the motherboard, memory, disk, etc. You can use WMI to communicate with the Windows OS, the hardware devices, running processes, COM+ components, etc. In this article we will discuss the basics of WMI and then explore how we can make use of WMI in our applications.

Windows Management Instrumentation (WMI)

Windows Management Instrumentation (WMI) provides a robust, low-level communication with the Windows Operating System from the managed environment. You can use WMI to communicate with the system’s hardware and still leverage the benefit of the CLR's managed environment, automated memory management, garbage collection, etc.  Actually, Windows Management Instrumentation (WMI) provides a set of extensions to the Windows Driver Model. These extensions can be accessed from the managed environment.

WMI includes the following providers:

Win32 Provider

WDM Provider

Event Log Provider

Registry Provider

SNMP Provider

View Provider

Performance Counter Provider

Active Directory Provider

Windows Installer Provider

The WMI Query Language

The WMI Query Language, a subset of the American National Standard Query Language (ANSI SQL), is much the same as any other standard query language and is used to retrieve system management information.

The following query retrieves the information of all the logical disk drives in your system.

Select * from Win32_LogicalDisk

To retrieve processor details in your computer system, you’ll need to write the following query:

SELECT * FROM Win32_Processor

Note that all WMI classes follow a language format known as the Managed Object Format (MOF).

To use WMI in your applications, you’ll need to include the following namespaces:




Programming WMI

In this section we will explore WMI – we will use WMI to retrieve CPU ID, MAC ID and Volume Serial Number of a drive in your system. The CPU ID identifies the CPU of the computer's central processor. The information includes model number, processor family, cache size, clock speed, and also the manufacturer codename. The Media Access Control or MAC address is the Network Interface Card address. It is composed of 12 hexadecimal characters (0-9, A-F) and is defined as a unique identifier used to identify most network equipment. Note that every network device has a unique MAC address. The Volume Serial Number of a drive is a running serial number that is generated for every format of the drive.

The following code snippet illustrates how you can display the drive information of your system:

ManagementObjectSearcher managementObjectSearcherQuery = new ManagementObjectSearcher
    ("SELECT * From Win32_LogicalDisk ");
ManagementObjectCollection managementObjectCollectionQueryCollection = managementObjectSearcherQuery.Get(); 
foreach ( ManagementObject managementObject in managementObjectCollectionQueryCollection) 
    switch (int.Parse( managementObject["DriveType"].ToString())) 
        case Removable: //Removable drives 
        case LocalDisk: //Local drives 
        case CD: //CD drives 
        case Network: //Network drives 
      Console.WriteLine("Drive: " + managementObject["Name"].ToString()); 

To retrieve the volume serial number of any drive in your system you can write the following code:

                                public string GetVolumeSerial(string strDriveLetter)
                if( strDriveLetter=="" || strDriveLetter==null) strDriveLetter="C";
                ManagementObject disk = 
                     new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
                return disk["VolumeSerialNumber"].ToString();

To retrieve the MAC address of the first network card in your system, you can write the following code;

                                public string GetMACAddress()
                ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
                string MACAddress=String.Empty;
                foreach(ManagementObject managementObject in managementObjectCollection)
                           if((bool)managementObject["IPEnabled"] == true) 
                           MACAddress= managementObject["MacAddress"].ToString() ;
                MACAddress = MACAddress.Replace(":","");
                return MACAddress;

To retrieve the processor id of the first processor, you can write the following code:

                                public string GetProcessorID()
                string cpuInfo =  String.Empty;
                ManagementClass managementClass = new ManagementClass("Win32_Processor");
                ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
                foreach(ManagementObject managementObject in managementObjectCollection)
                         cpuInfo = managementObject.Properties["ProcessorId"].Value.ToString();                         
                return cpuInfo;



WMI, an implementation of the WBEM standards, is a key component of Microsoft Windows management services and provides a framework for accessing system level information from the managed environment. This article presented an overview of WMI, WMI Query Language and an idea on how we can leverage WMI in our applications. Happy reading!

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved