Working with Windows Management Instrumentation

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:

System.Diagnostics

System.Management

System.Management.Instrumentation

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 
            break; 
        case LocalDisk: //Local drives 
            break; 
        case CD: //CD drives 
            break; 
        case Network: //Network drives 
            break; 
        default:
            break; 
    } 
      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 +":\"");
                disk.Get();
                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(MACAddress==String.Empty)  
                     {
                           if((bool)managementObject["IPEnabled"] == true) 
                           MACAddress= managementObject["MacAddress"].ToString() ;
                     }
                     managementObject.Dispose();
                }
                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)
                {
                     if(cpuInfo.Equals(String.Empty))
                     {
                         cpuInfo = managementObject.Properties["ProcessorId"].Value.ToString();                         
                     }
                }
                return cpuInfo;
           }
 

References

http://aspalliance.com/629_Introducing_Windows_Management_Instrumentation_WMI#Page1

http://www.codeproject.com/Articles/54064/Working-With-Windows-Management-Instrumentation-WM

http://msdn2.microsoft.com/en-us/library/ms257361(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms811553.aspx

Conclusion

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!



Related Articles

Comments

  • Wholesale Oakley M Frame sale for cheap

    Posted by qpetssjbe on 06/25/2013 06:46am

    Cheap Ray Bans ,Oakley sunglasses created to change each year, exactly like another style?? However, your choices are very different game situations, there is no truly fashion. Product design or collection of materials, happen to be some advanced scientific experiments and testing to guarantee its comfort and excellent, high level of integration as well as the function and fashion. Cheap Oakley Sunglasses ,Oakley sunglasses, adjusted for changes in per year resembles other ways. However, the alternatives are very different, there isn't any real fashion lovers the prospect. That summertime is resulting, it will always be a better plan, looking to find the many outdoor activities, protection, and cheap Oakley sunglasses. Oakley Holbrook ,Oakley sunglasses are not only steer clear of the sun, however the other hit the decorative surface. The trendy appearance and fashion style, it's going to be your sensible choice. How you can protect their eyes? The correct answer is in glasses. Select the sunglasses, beyond just the sunglasses can protect you, it ought to be a fashion and lifestyle. Know used sheet metal sunglasses, for a period of time could be slightly deformed, the industry normal phenomenon. Dealer Executive to adjust to. Oakley sports sunglasses better sunglasses sold to locate. You'll find a framework and coated lenses to adjust to every face type. From one to exist in a sun will not take away the hazard, however marked complexion and eyes from the proliferation of lengthy, long-term harm to UV to lessen a person's eye, which became stop the harm for the eye compared to the amount of ultraviolet illumination from the individual's life may be great for. If your UV radiation intensity is just right visual turbidity, it can be the result of the well-being in the ultraviolet light of solar power. Oakley sunglasses shape and metal metal injection molding, developing a convenient button to discharge each shot, the handle to the smallest detail. A contemporary gradient with Oakley sunglasses for female: they provide 100% Ultraviolet shield, luxury and ambitions are clear. Whether you should modify?? You to adjust to your sunglasses, which may be done by a similar company, it may guarantee a similar service quality, mixing style and practicality. Oakley sunglasses, you'll pick a qualified sunglasses, Oakley sunglasses popular Oakley sunglasses are selling on earth.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds