The power of WMI with the .Net Framework
Many developers are not familiar with WMI and therefore don’t make use of this powerful technology. WMI stands for Windows Management Instrumentation. WMI core is already a part of windows ME/2000 and XP.
The purpose of WMI is to provide a standardized means for managing your computer system, be it a local computer or all the computers in an enterprise. In its simplest term, management is little more than the collecting of data about the state of a managed object on a computer system and altering that by changing the data stored about the object. A managed object can be a hardware entity such as a memory array, port, or disk drive. It can also be a software entity, such as a service, user account, or page file.
In managing a hard disk, you can use WMI to monitor the amount of free space remaining on the disk. You could also use WMI to remotely alter the state of the drive by deleting files, changing file security, partitioning or formatting the drive. Using the WMI framework, you can create a management application that monitors an enterprise, provides event-based alerts, and allows a user to control different aspects of the enterprise.
Inside the management namespace
ManagementObject
The ManagementObject class represents a data management object, which is an instance of a management class from the Common Information Model. ManagementObject is derived from ManagementBaseObject, therefore it allows access to the Properties and Qualifiers Collections. Additional information contained in this class are the Scope (where you are connected and which credentials are you using), the connection Options, and a few others. Some very interesting parts of the ManagementObject are the public methods Get, Put, and InvokeMethod, which you use to bind to the management object, to save changes, and to invoke methods of the object. You can also access associated management objects (e.g., users are associated with groups) with the two public methods GetRelated and GetRealtionship.
ManagementObjectCollection
The ManagementObjectCollection represents different collections of WMI instances like management objects, namespaces, scopes, and query watcher. You use this class to enumerate instances of management classes. For example, one management class represents a Windows service but many instances of the class can exist. For each service installed on your machine, there is one management object of type Win32_Service. You will obtain this collection of running instances from your management class to obtain the details of all services installed on your machine.
ManagementClass
The ManagementClass is derived from ManagementObject, and it represents a management class from the Common Information Model. You can use this class to obtain all instances of ManagementObjects in a ManagementObjectCollection by calling the GetInstances method or to create new instances by calling the CreateInstance Method. It also contains a property Methods, which gets or sets a collection of MethodData objects that represent the methods defined in the WMI class.
The System.Management namespace contains many more classes, but they are far beyond the scope of this article. For further information take a look at the MSDN Library.
Enter the code
Now that we know more about WMI and its implementation in the .NET Framework, we can write our first sample application.
You will need to add System.Managment as a reference (right click the references folder under your solution in VS and select System.Managment under the .NET tab).
using System.Management;
We will now use WMI to retrieve the Processors ID.
string cpuInfo = "";
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
cpuInfo += "CPU ID - " + mo.Properties["ProcessorId"].Value.ToString() + "\n";
cpuInfo += "CPU Load - " + mo.Properties["LoadPercentage"].Value.ToString() + "%" + "\n";
cpuInfo += "L2 Cache - " + mo.Properties["L2CacheSize"].Value.ToString() + "KB" + "\n";
}
We can use the Management Object Searcher to access the WMI information as well; it uses WQL query’s to obtain the appropriate information.
//string to store Drive info
string sDriveInfo = "";
//Option for the connection
ConnectionOptions co = new ConnectionOptions();
//You must supply as valid username and password for the remote computer
co.Username = "Admin";
co.Password = "123";
//The scope to use
ManagementScope ms = new ManagementScope("\\\\" + sComputer + "\\root\\cimv2", co);
//get drive collection
ObjectQuery oq = new ObjectQuery("Select * From Win32_LogicalDisk where DriveType = '3'");
ManagementObjectSearcher mos = new ManagementObjectSearcher(ms,oq);
ManagementObjectCollection moc = mos.Get();
//loop through each object to get drive information
foreach(ManagementObject mo in moc)
{
sDriveInfo += "Drive " + mo["Name"].ToString();
}
Conclusion
Well, that's it. Hopefully, this will encourage you to drill down into the WMI technology and take advantage of it. The WMI has so much information in it that the uses for it are almost limitless from performance monitors to hardware detection and custom editors.
Download: Source Code