Introduction
WMI is a technology introduced by Microsoft to support the management of operating systems and system objects in an enterprise. WMI allows developers to use a simple, consistent mechanism to query for information or configure settings on computers or enumerate objects on computers across an enterprise. In my project, I had to enumerate the different objects like network adapters, printers, and CPUs on a machine. In the absence of WMI , I might have to learn a lot of APIs and perform a lot of research, atleast one for each object type to be enumerated. With WMI, my life became simple as every object on a machine can be enumerated as a class using WMI.
Explaining more on WMI is beyond the scope of this article. There is no proper documentation available on WMI, so developers are not very much aware of this. This article is designed to introduce enumerating system objects and their characteristics using WMI to developers, which I had difficulty finding. A tool to help browse the information that's available through WMI is available with a test application called WBEMTest.exe that gets shipped with every version of an operating system. This is a very powerful app, since it provides access to almost every DCOM interface exposed by WMI, but it is very difficult for a novice to understand.
WMI works on almost all Microsoft ® platforms. However if
WMI is not available in any operating system, one can download "Windows Management Instrumentation (WMI) CORE 1.5 (Windows 95/98/NT 4.0)".
In the .NET Framework, WMI is exposed in the System.Management namespace. One can use this namespace to perform various tasks using WMI.
Getting Started
WMIDemo can be used for getting simple computer information. This contains WMIHelper which exposes several methods which can be used to get specific information of the system area. Basic entity is also included in the library. These can be used for getting and displaying the computer information in any C# project.
BIOS - Stores information related to BIOS
ComputerSystem - Stores information related to computer system
Disk - Stores information related to physical disk
HotFixes - Stores information related to the HotFixes installed in the system
LogicalDisk - Stores information related to logical disk drives
NetworkAdapter - Stores information related to network adapters
OS - Stores information related to operating system
Printers - Stores information of printers installed
Processors - Stores information of processors
SoundCard - Stores information related to soundcards installed in the system
VideoController - Stores information related to videocards installed in the system
WMIHelper class exposes static methods which return the filled structures for direct use. The class is made static so there is no need for instantiation. Exposed methods are listed below:
FillHotFixes - Finds information related to hot fixes and returns object of HotFixes class containing the information
FillComputerInformation - Finds information related to computer information and returns object of CompueterSystem class containing the details
FillProcessorInformation - Finds information related to processors and returns object of Processors class containing the details
FillBiosInformation - Finds information related to BIOS and returns object of BIOS class containing the details
FillOSInformation - Finds information related to operating system and returns object of OS class containing the details
FillVideoController - Finds information related to Video Controller and returns object of VideoController class containing the details
FillSoundCard - - Finds information related to sound cards and returns object of SoundCard class containing the details
FillDisks - Finds information related to physical disk drive and returns object of Disk class containing the details
FillLogicalDisks - Finds information related to logical disk drives and returns object of LogicalDisk class containing the details.
FillPrinters - Finds information related to printers and returns object of Printers class containing the details
FillNetworkAdapter - Finds information related to network adapters and returns object of NetworkAdapter class containing the details
Using the Code
Use of this module is very simple. You have to call the required method directly from the class name. No instantiation is required.
WMIHelper.FillComputerInformation(machineName, userName, password, queryAres)
machineName - Name of machine which is to be queried
userName - Username of any authenticated user for the machine machineName
password - Password for the provided userName
queryAres - Area to be queried - this can be found on the DescriptionAttribute of the member in the enum QueryArea
For local machine,
local or
machineName can be used directly. For local machine, do not specify
username and
password. For domain user,
username will be
DOMAIN\USERNAME.
Brief Inside Code
This module uses ManagementClass for retrieving information which is part of managed .NET and can be found under the System.Management namespace. You will need to add a reference to System.Management.dll.
You will need to create a connection by creating an instance of ConnectionOptions and declare the scope using ManagementScope. Specify the query path using the ManagementPath instance.
path = @"\\" + machineName + @"\root\cimv2";
if (userName != string.Empty && password != string.Empty)
{
ConnectionOptions options = new ConnectionOptions();
options.Username = userName;
options.Password = password;
mScope = new ManagementScope();
mScope.Options = options;
mScope.Path = new ManagementPath(path);
}
You are done. Now instantiate ManagementClass, assign scope and run the query managementClass.GetInstances().
ManagementClass managementClass = new ManagementClass(path + ":" + queryArea);
if (mScope != null)
{
managementClass.Scope = mScope;
}
ManagementObjectCollection instances = managementClass.GetInstances();
if (instances == null)
{
return null;
}
foreach (ManagementBaseObject instance in instances)
{
}
References
History
23rd April, 2007: This is the first release of the code.
Modifications and feature enhancements will be done on request. I will request users to email me directly for any suggestions.