How To: (Almost) Everything In WMI via C# - Part 3: Hardware
Code https://files.cnblogs.com/files/mqingqing123/WMI_Hardware.zip
This is the third article in the series of articles How To: (Almost) Everything In
WMI
via C#.
- Reference Part 1: WIN32_Registry
- Reference Part 2: WIN32_Process
- Reference Part 3: Hardware Property Aggregation
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.
-
: Mother board or System BoardWin32_BaseBoard
-
: System BatteryWin32_Battery
-
: System BIOSWin32_BIOS
-
: Physical System BusWin32_Bus
-
: System Optical DrivesWin32_CDROMDrive
-
: System DisksWin32_DiskDrive
-
: System DMA ChannelsWin32_DMAChannel
-
: System FanWin32_Fan
-
: System Floppy ControllersWin32_FloppyController
-
: System Floppy DrivesWin32_FloppyDrive
-
: System IDE ControllersWin32_IDEController
-
: System IRQ ResourcesWin32_IRQResource
-
: System KeyboardWin32_Keyboard
-
: System MemoryWin32_MemoryDevice
-
: Network AdaptersWin32_NetworkAdapter
-
: Adapter configurationWin32_NetworkAdapterConfiguration
-
: Common Devices built into the System boardWin32_OnBoardDevice
-
: The Parallel portsWin32_ParallelPort
-
: The PCMCIA Laptop busWin32_PCMCIAController
-
: Storage Media such as tapes, etc.Win32_PhysicalMedia
-
: The physical memory deviceWin32_PhysicalMemory
-
: Physical ports such as DB-25 pin male, PS/2, etc.Win32_PortConnector
-
Win32_Bus
-
: I/O ports on a systemWin32_PortResource
-
: Plain Old Telephone System Modem DevicesWin32_POTSModem
-
: Processor specificationsWin32_Processor
-
: System SCSI busWin32_SCSIController
-
: Serial PortsWin32_SerialPorts
-
: Port ConfigurationWin32_SerialPortConfiguration
-
: Sound DevicesWin32_SoundDevice
-
: System DetailsWin32_SystemEnclosure
-
: Physical Tape DrivesWin32_TapeDrive
-
: Heat StatisticsWin32_TemperatureProbe
-
: UPS detailsWin32_UninterruptiblePowerSupply
-
: USB Controller on a systemWin32_USBController
-
: USB HubWin32_USBHub
-
: Physical Video ControllerWin32_VideoController
-
: Voltage StatisticsWin32_VoltageProbe
Using the Attached Code
Methods (Local Machine or Remote Machine)
-
- Gets theGetPropertyValues()
Class Properties values of the objectWMI
Instantiate the Local Provider
Hide Copy Code
//Local Connection
Connection wmiConnection = new Connection();
Instantiate the Remote Provider
//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()
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:
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.