Click here to Skip to main content
15,881,281 members
Articles / Programming Languages / C#
Article

Enumerate System Objects using WMI

Rate me:
Please Sign up or sign in to vote.
4.82/5 (29 votes)
23 Apr 2007CPOL4 min read 89.2K   4.5K   75   10
This article demonstrates how to query system using WMI interface
Screenshot - WMIQuery1.png

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.

C#
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.

Screenshot - WMIQuery2.png

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.

C#
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().

C#
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)
{
    //Fill information
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralWMI query doesnot return record for user having less previleges!! Pin
Cracked-Down2-Mar-09 19:50
Cracked-Down2-Mar-09 19:50 
GeneralGreat Article.. Really Useful Pin
wabreyka7-Jan-09 16:00
wabreyka7-Jan-09 16:00 
Questionservice pack information Pin
yadhvi15-Sep-08 2:19
yadhvi15-Sep-08 2:19 
Generalempty password for session user Pin
jamalhaider16-Jun-08 3:38
jamalhaider16-Jun-08 3:38 
QuestionAccessing Access-Point Data via WMI Pin
sam[CH][HSR]5-Jun-07 5:27
sam[CH][HSR]5-Jun-07 5:27 
QuestionCode Requirements Pin
Nhilesh B20-May-07 4:09
Nhilesh B20-May-07 4:09 
GeneralGood article, thanks Pin
ersincelik4-May-07 3:29
ersincelik4-May-07 3:29 
JokePassword Pin
Deepak Raj25-Apr-07 18:45
Deepak Raj25-Apr-07 18:45 
AnswerRe: Password Pin
Manish Ranjan Kumar25-Apr-07 20:10
professionalManish Ranjan Kumar25-Apr-07 20:10 
This Demo is not for the purpose of explaining the windows application. It's scope is defined under explaining the WMI to make query.
JokeRe: Password Pin
Deepak Raj25-Apr-07 21:13
Deepak Raj25-Apr-07 21:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.