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

How To: (Almost) Everything In WMI via C# - Part 3: Hardware

Rate me:
Please Sign up or sign in to vote.
4.67/5 (44 votes)
3 Apr 2007CPOL3 min read 295.4K   12.7K   145   29
An Extensible Framework for enumerating WMI Class Properties
Screenshot - wmi01.jpg

Introduction

This is the third article in the series of articles How To: (Almost) Everything In WMI via C#.

In this article, I'm going to introduce a simple framework that can be used to aggregate property values for any of the hundreds of classes in the WMI namespace. In the attached example, I have included 30+ hardware classes used for everything from enumerating properties on network client CDRom disk drives to USB Host Controllers. When it comes to probing machines on the network for very detailed hardware information, nothing on the street can compare with WMI.

In the previous additions, we focused on a way to provide the property values as well as executing WMI methods. In this article however, we are going to focus on the considerable wealth of knowledge we can obtain by the properties alone.

Included Classes

The following included classes will return a collection of all the properties and values. This only applies to single string attributes. If you're going to need to aggregate multistring[] attribute values, then you'll need to wrap that in addition. Noticeably absent from this class listing is Win32_Printer, which I have left out since it deserves its own article.

  • Win32_BaseBoard: Mother board or System Board
  • Win32_Battery: System Battery
  • Win32_BIOS: System BIOS
  • Win32_Bus: Physical System Bus
  • Win32_CDROMDrive: System Optical Drives
  • Win32_DiskDrive: System Disks
  • Win32_DMAChannel: System DMA Channels
  • Win32_Fan: System Fan
  • Win32_FloppyController: System Floppy Controllers
  • Win32_FloppyDrive: System Floppy Drives
  • Win32_IDEController: System IDE Controllers
  • Win32_IRQResource: System IRQ Resources
  • Win32_Keyboard: System Keyboard
  • Win32_MemoryDevice: System Memory
  • Win32_NetworkAdapter: Network Adapters
  • Win32_NetworkAdapterConfiguration: Adapter configuration
  • Win32_OnBoardDevice: Common Devices built into the System board
  • Win32_ParallelPort: The Parallel ports
  • Win32_PCMCIAController: The PCMCIA Laptop bus
  • Win32_PhysicalMedia: Storage Media such as tapes, etc.
  • Win32_PhysicalMemory: The physical memory device
  • Win32_PortConnector: Physical ports such as DB-25 pin male, PS/2, etc.
  • Win32_Bus: Physical System Bus
  • Win32_PortResource: I/O ports on a system
  • Win32_POTSModem: Plain Old Telephone System Modem Devices
  • Win32_Processor: Processor specifications
  • Win32_SCSIController: System SCSI bus
  • Win32_SerialPorts: Serial Ports
  • Win32_SerialPortConfiguration: Port Configuration
  • Win32_SoundDevice: Sound Devices
  • Win32_SystemEnclosure: System Details
  • Win32_TapeDrive: Physical Tape Drives
  • Win32_TemperatureProbe: Heat Statistics
  • Win32_UninterruptiblePowerSupply: UPS details
  • Win32_USBController: USB Controller on a system
  • Win32_USBHub: USB Hub
  • Win32_VideoController: Physical Video Controller
  • Win32_VoltageProbe: Voltage Statistics

Using the Attached Code

Methods (Local Machine or Remote Machine)

  • GetPropertyValues() - Gets the WMI Class Properties values of the object

Instantiate the Local Provider

C#
//Local Connection
Connection wmiConnection = new Connection(); 

Instantiate the Remote Provider

C#
//Remote Connection
//Requires UserName, Password, Domain, and Machine Name
Connection wmiConnection = new Connection("neal.bailey",
                                          "3l!t3p@$$",
                                          "BAILEYSOFT",
                                          "192.168.2.100"); 

Using the Classes

Using the classes is very simple. You just create a new instance of the WMI connection and then send the connection object into the WMI class you want to use. All the classes have the single method GetPropertyValues().

C#
Connection wmiConnection = new Connection(); //The local or remote connection object
Win32_Battery b = new Win32_Battery(wmiConnection); //Create the WMI Class you want

    foreach (string property in b.GetPropertyValues()) //enumerate the collection
    {
         Console.WriteLine(property);
    }

Extending To Add Your Own WMI Classes

This solution is a simple framework that anyone can use to add and remove the WMI classes they need to enumerate.

Step One: Find some classes you want from the Microsoft WMI SDK.

Step Two: Create a class with the same name as the WMI class (example Win32_Printer) and paste the following code into it:

C#
using System;
using System.Collections.Generic;
using System.Text;

namespace baileysoft.Wmi
{
    class Win32_Printer : IWMI //The class name is the ONLY part that changes
    {
        Connection WMIConnection;

        //The class name is the ONLY part that changes
        public Win32_Printer(Connection WMIConnection) 
        {
            this.WMIConnection = WMIConnection;
        }
        public IList{string} GetPropertyValues() //replace curly braces with <> 
        {
            string className = System.Text.RegularExpressions.Regex.Match(
                                  this.GetType().ToString(), "Win32_.*").Value;

            return WMIReader.GetPropertyValues(WMIConnection,
                                               "SELECT * FROM " + className,
                                               className);
        }
    }
}

Step Three: Create a new tag in the settings.xml file for your class and enter the properties you want to enumerate (those ones in the SDK). Refer to the settings.xml file for the existing examples.

Conclusion

The WMI (Windows Management Instrumentation) provider is considerably slower than the native .NET classes and it may be easier to do this all with .NET classes but there are a lot of commercial solutions out there that do this exact same thing and surprise, surprise they use WMI for it. The included example solution demonstrates how to create a very powerful, extensible WMI hardware management solution.

History

  • Submitted: 03 April, 2007
  • Fixed formatting and grammar: 04 April, 2007

License

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


Written By
Software Developer
United States United States
I'm a professional .NET software developer and proud military veteran. I've been in the software business for 20+ years now and if there's one lesson I have learned over the years, its that in this industry you have to be prepared to be humbled from time to time and never stop learning!

Comments and Discussions

 
Questionbaileysoft Pin
Member 1248760910-Nov-19 18:12
Member 1248760910-Nov-19 18:12 
QuestionSettings XML Pin
kohtianan29-Oct-15 17:07
kohtianan29-Oct-15 17:07 
AnswerRe: Settings XML Pin
thund3rstruck30-Oct-15 5:14
thund3rstruck30-Oct-15 5:14 
QuestionTruely nice Pin
Naresh.Holla23-Oct-15 22:12
Naresh.Holla23-Oct-15 22:12 
Questionregarding bios boot order Pin
vinayagashiva1237-Oct-14 10:24
vinayagashiva1237-Oct-14 10:24 
QuestionDetection of a physical keyboard Pin
Jenny D29-May-14 12:02
Jenny D29-May-14 12:02 
QuestionThank you Pin
Silverboy032522-Sep-13 21:36
Silverboy032522-Sep-13 21:36 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey25-Apr-12 0:24
professionalManoj Kumar Choubey25-Apr-12 0:24 
Nice
GeneralMy vote of 5 Pin
onefootswill5-Jan-12 22:48
onefootswill5-Jan-12 22:48 
Questionyou know you can loop on an interface, right? Pin
The-Great-Kazoo29-Mar-11 9:11
professionalThe-Great-Kazoo29-Mar-11 9:11 
GeneralCool! Pin
philippe dykmans20-Oct-10 10:49
philippe dykmans20-Oct-10 10:49 
GeneralRe: Cool! Pin
thund3rstruck20-Oct-10 11:02
thund3rstruck20-Oct-10 11:02 
QuestionGetting the Priter Status gives me Idle Pin
onkar311814-Oct-10 3:43
onkar311814-Oct-10 3:43 
GeneralGet Windows product ID Pin
KSagar11-Aug-10 2:49
KSagar11-Aug-10 2:49 
QuestionCan I get the CPU voltage & fan speed (system fan , cpu fan...) via this library ? Pin
polochen12-Aug-09 23:02
polochen12-Aug-09 23:02 
GeneralEasier way to get the class name Pin
jarvisa14-Jun-09 23:31
jarvisa14-Jun-09 23:31 
GeneralWMI query doesnot return record for user having less previleges!! Pin
Cracked-Down2-Mar-09 19:50
Cracked-Down2-Mar-09 19:50 
Generala runtime error in this app [modified] Pin
moharrami3-Nov-08 0:04
moharrami3-Nov-08 0:04 
QuestionHow can I find a USB printer on a network...please help Pin
Raju Gaddam6-Aug-08 19:32
Raju Gaddam6-Aug-08 19:32 
Questionhow can I get 3G device information Pin
alert091928-Jul-08 17:21
alert091928-Jul-08 17:21 
GeneralUPS power Pin
phuong oanh19-Apr-08 22:50
phuong oanh19-Apr-08 22:50 
QuestionCan I use WMI to modify these property? Pin
fitatc18-Oct-07 15:01
fitatc18-Oct-07 15:01 
AnswerRe: Can I use WMI to modify these property? Pin
thund3rstruck19-Oct-07 2:58
thund3rstruck19-Oct-07 2:58 
AnswerRe: Can I use WMI to modify these property? Pin
Cool Cassis8-Nov-07 7:56
Cool Cassis8-Nov-07 7:56 
GeneralThanks you Pin
danieltang7-Oct-07 23:08
danieltang7-Oct-07 23:08 

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.