Windows Management Instrumentation (WMI) is the infrastructure for management
data and operations on Windows-based operating systems. With WMI you can get
information on all hardware in a system, including:
- HDD Serial Number(s)
- HDD Sizes
- HDD Free Space
- CPU Serial Number(s)
- CPU Clock Speed
- CPU Socket Type
- Network Adapter MAC Address
- Network Adapter Default Gateway
And much more. In this tutorial we will be looking at some of the information
that can be retrieved using WMI, along with Windows Query Language,
or WQL. though this tutorial isn't an all
inclusive tutorial on the topic, we will look at
enough to at least give you a good foundation for using WMI in your
applications.
In this tutorial we will see how to retrieve HDD information, CPU information,
and Network Adapter information. For this tutorial there are 3 classes we will
be using to retrieve this information for our applications:
Of course there are many more WMI Classes that are available, and if I were to
try and cover all of them this would be a 20 page tutorial. All the items we
will be using require a reference to the System.Management Namsepace provided in the .Net framework
Win32_LogicalDiskClass
The Win32_LogicalDiskāWMI class represents a data source that resolves to an
actual local storage device on a computer system running Windows. In this
section we will do a few tasks such as:
- Retrieve selected HDD serial number
- Retrieve selected HDD free space
- Retrieve HDD initial size
The first task, getting the serial number of a selected HDD (as with all the
tasks in this section) we will be passing a drive letter to our method, with
that drive letter we will query the system using the DeviceIDProperty
of the Win32_LogicalDisk class. Once we have done that we have access to many
properties, which we will use in this section. First, to receive the serial
number:
02 |
/// method to retrieve the selected HDD's serial number |
04 |
/// <param name="strDriveLetter">Drive letter to retrieve serial number
for</param> |
05 |
/// <returns>the HDD's serial number</returns> |
06 |
public string GetHDDSerialNumber( string drive) |
10 |
if (drive
== "" ||
drive == null ) |
16 |
ManagementObject
disk = new ManagementObject( "Win32_LogicalDisk.DeviceID=\"" +
drive + ":\"" ); |
20 |
return disk[ "VolumeSerialNumber" ].ToString(); |
As you can see, we check the DevideID to ensure we are looking at the proper
HDD, then we use the Get Method of the ManagementObject Class to bind our management object,
allowing us to retrieve the attributes we are looking for.
The nice thing about the Win32_LogicalDisk Class is once you learn to retrieve
one attribute or property, the rest are easily retrieved, such as retrieving the
free space of a HDD:
02 |
/// method to retrieve the HDD's freespace |
04 |
/// <param name="drive">Drive letter to get free space from (optional)</param> |
05 |
/// <returns>The free space of the selected HDD</returns> |
06 |
public double GetHDDFreeSpace( string drive) |
10 |
if (drive
== "" ||
drive == null ) |
16 |
ManagementObject
disk = new ManagementObject( "Win32_LogicalDisk.DeviceID=\"" +
drive + ":\"" ); |
20 |
return Convert.ToDouble(disk[ "FreeSpace" ]); |
And retrieving the initial size of a selected HDD, like so:
02 |
/// method to retrieve the HDD's size |
04 |
/// <param name="drive">Drive letter to get free space from (optional)</param> |
05 |
/// <returns>The free space of the selected HDD</returns> |
06 |
public double getHDDSize( string drive) |
10 |
if (drive
== "" ||
drive == null ) |
16 |
ManagementObject
disk = new ManagementObject( "Win32_LogicalDisk.DeviceID=\"" +
drive + ":\"" ); |
20 |
return Convert.ToDouble(disk[ "Size" ]); |
You will notice in the previous 3 methods the drive letter is actually optional.
We check to see if a drive was provided, and if it wasnt then we default it to
the users "C" drive, which is usually the OS drive on most computers.
Win32_NetworkAdapterConfiguration Class
The Win32_NetworkAdapterConfiguration WMI class represents the attributes and
behaviors of a network adapter. This class includes extra properties and methods
that support the management of the TCP/IP and IPX (Internetwork Packet Exchange)
protocols.
With this class we can get information such as:
- MAC Address
- Default IP Gateway
- IP Enabled Status
In the following examples we will be introduced to the ManagementObjectCollection Class.
This class represents different collections of management objects that can be
retrieved through WMI. We will then loop through all the items in our collection
to retrieve information about the network adapter on that particular system.
First we will look at how to retrieve the systems MAC address. In these examples
we will onl;y be retrieving information from the first network adapter we find,
they can easily be modified to create a collection of network adapter
information.
02 |
/// Returns MAC Address from first Network Card in Computer |
04 |
/// <returns>MAC Address in string format</returns> |
05 |
public string FindMACAddress() |
10 |
ManagementClass
mgmt = new ManagementClass( "Win32_NetworkAdapterConfiguration" ); |
12 |
ManagementObjectCollection
objCol = mgmt.GetInstances(); |
13 |
string address
= String.Empty; |
15 |
foreach (ManagementObject
obj in objCol) |
17 |
if (address
== String.Empty) |
24 |
if (( bool )obj[ "IPEnabled" ]
== true ) |
26 |
address
= obj[ "MacAddress" ].ToString(); |
34 |
address
= address.Replace( ":" , "" ); |
In the first example, along with the others in this section, we will be using
the GetInstances Method of the ManagementObjectCollection
Class to retrieve all instances of the specified class, in this case the
Win32_NetworkAdapterConfiguration class.
Now lets look at retrieving the default IP gateway of a network adapter. As with
the first example, we will be using the first adapter we find, as long as its IPEnabled
Property is true:
02 |
/// method to retrieve the network adapters |
03 |
/// default IP gateway using WMI |
05 |
/// <returns>adapters default IP gateway</returns> |
06 |
public string GetDefaultIPGateway() |
11 |
ManagementClass
mgmt = new ManagementClass( "Win32_NetworkAdapterConfiguration" ); |
13 |
ManagementObjectCollection
objCol = mgmt.GetInstances(); |
14 |
string gateway
= String.Empty; |
16 |
foreach (ManagementObject
obj in objCol) |
18 |
if (gateway
== String.Empty) |
25 |
if (( bool )obj[ "IPEnabled" ]
== true ) |
27 |
gateway
= obj[ "DefaultIPGateway" ].ToString(); |
|