Skip to main content

How to use WMI from a .NET Application

To start with, let us see what is WMI and what it offers. WMI is an acronym for Windows Management Instrumentation, which is basically an interface to the Windows OS system settings, drivers, and parameters. It also allows managing Windows personal computers and servers through it.

A .NET developer can use WMI to obtain information about drivers installed on the client machine, verify whether the system is licensed or not, check for hardware configuration and a lot more.

Quoting Linus Torvalds, “Talk is cheap. Show me the code”, let’s get to the basics of WMI usage. To get data through WMI, a SQL-like query is used. The specific query type is called WQL (WMI Query Language). Don’t let the name confuse you. It is still very similar to SQL.

Before diving into code, you should know that Windows comes with a tool called WMI Test Tool, which lets you test WQL queries, to check their correctness and returned results. It is a bit harder to track wrong query results in code, so this tool can save some time for the developer. To run it, just start the Run dialog (or the Command Prompt) and type wbemtest.

Once it is started, you will see a window like this:


Click on Connect and you will see a dialog like this:




It lets you connect to a namespace on your local Windows computer. You can use your credentials (although for most queries this is not a requirement) and select the impersonation and authentication levels (once again, for most queries the default settings are acceptable). Once you click connect, you will be able to execute WMI queries, as well as perform other tasks (for example, enumerate classes in a superclass to review its possibilities).

Before creating a query, you need to understand what information you want to obtain. The query is executed against a WMI class – you can read the complete list here. Let’s take the Win32_Processor class as an example here. Querying against this class will give us information about the CPU installed on a machine. If the machine runs with multiple CPUs, a query result will be returned for each one of them.

The Win32_Processor class exposes the following properties:
AddressWidth
Architecture
Availability
Caption
ConfigManagerErrorCode
ConfigManagerUserConfig
CpuStatus
CreationClassName
CurrentClockSpeed
CurrentVoltage
DataWidth
Description
DeviceID
ErrorCleared
ErrorDescription
ExtClock
Family
InstallDate
L2CacheSize
L2CacheSpeed
L3CacheSize
L3CacheSpeed
LastErrorCode
Level
LoadPercentage
Manufacturer
MaxClockSpeed
Name
NumberOfCores
NumberOfLogicalProcessors
OtherFamilyDescription
PNPDeviceID
PowerManagementCapabilities[]
PowerManagementSupported
ProcessorId
ProcessorType
Revision
Role
SocketDesignation
Status
StatusInfo
Stepping
SystemCreationClassName
SystemName
UniqueId
UpgradeMethod
Version
VoltageCaps

Most of these are have self-descriptive names, but if you are ever confused about one of them, you can always refer to the MSDN documentation for the class, that explains each one of them.

Now, let’s try to get the values of the above mentioned properties in your .NET application. In my examples I am using C#, but if you are using another .NET language, you shouldn’t have a problem adapting the code.

First, you need to add a reference to the System.Management and System. Management.Instrumentation namespaces. This is done by right-clicking on References in the Solution Explorer and selecting Add Reference. Then, you can select the above mentioned libraries from the .NET list:



Once selected, you need to reference the proper namespaces in your code:using System.Management;
Now, to the actual code. I am going to create a function that can be called from anywhere in the code to simplify this task.void GetCPUInfo()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
foreach (ManagementObject obj in searcher.Get())
{
if (!(obj == null))
Debug.Print(obj.Properties["CpuStatus"].Value.ToString());
}
}


The ManagementObjectSearcher is the key element here – it gets the returned properties based on the query. The parameter I am passing to it when instantiating is the actual query. As you see, it is very similar to SQL. My current query will retrieve all properties available in Win32_Processor. I iterate through them (note that each result is a ManagementObject – the property holder, in this case will be a separate instance for each CPU that is found) and print in the Output window the value of the CpuStatus property:



The 1 here is exactly what is returned. It is a good practice to consult the documentation before reading specific properties, to understand the possible returned values. 1 for CpuStatus means that the CPU is installed and is active.

Important note: Some of the readers might be curious, why there is a null value verification. Some of the classes require user authentication to get the correct data and some properties are simply not available, being the cause of multiple exceptions, depending on the authentication methods and property types. Therefore, to avoid exceptions, this code security measure is used here.

If only one property is needed to be retrieved, then the query can be organized like this:SELECT CpuStatus FROM Win32_Processor


The important thing to remember here is that when you only retrieve one property, the rest of them are unavailable for that specific query result. Therefore, trying to get their value will cause an exception.



Courtesy: Original source is from Denzel D at - https://dzone.com/articles/how-use-wmi-net-application

Comments

Popular Posts

TechBytes on Linux

This is a growing list of Linux commands which might come handy for the newbies of Linux. 1. Found out i had to set the date like this:    # date -s 2007.04.08-22:46+0000 2. Mounting     sudo mount -t cifs // < pingable_host_or_ip > / < win_share_name > /build -o user= ,domain= ,uid=string,gid=string 3. To install linux packages from internet (ubuntu only)     apt-get install 4. To determine what ports the machine is currently listening on     netstat -an | grep -i listen | less 5. Find in files in Linux     find . | xargs grep 'string' -sl 6. To become superuser/root     sudo -i 7. To find a running process using name ps -aef | grep "searchstring" 8. Alt + F2 opens run window in RHEL 9. To access windows share from linux smb:// /d$ 10. To know the last reboot date & time $ last reboot | head -1 11. To install RPM packages in RHEL rpm -ivh 12. To un-install RPM package in RHEL rpm -e 13. To display Linux Kerne

How to change the default pdf viewer application in your Mac or MacBook

  To  set  a  PDF viewer  as the  default  on  Mac  OS X: Select any  PDF  file from Finder. Control-click to open the menu. ...  Choose Get Info from the menu that opens. ...  From the Open with: section in the new window that opens, select your preferred application, such as Adobe  Reader  or Preview. ...  Click the  Change  All button.

ಮ್ಯಾಕ್ ಬುಕ್ ಮತ್ತು ವಿನ್ಡೋಸ್ ಲ್ಯಾಪ್ ಟಾಪ್ ನಲ್ಲಿ ಎಲ್ಲ ಆಪ್ ಗಳಲ್ಲಿ ಕನ್ನಡ ಟೈಪ್ ಮಾಡುವುದು ಹೇಗೆ ?? How to type kannada on any app in Mac OS and Windows laptops

 ಮ್ಯಾಕ್ ಬುಕ್ ನಲ್ಲಿ ಕನ್ನಡ ದಲ್ಲಿ ಟೈಪ್ ಮಾಡಲು ಲಿಪಿಕ ಎನ್ನುವ ಈ ಕೆಳಗಿನ ಲಿಂಕ್ ಅನ್ನು ಕ್ಲಿಕ್ ಮಾಡಿ .pkg ಫೈಲ್ ಅನ್ನು ಡೌನ್ ಲೋಡ್ ಮಾಡಿ ಅದನ್ನು ಇನ್ಸ್ತಾಲ್ ಮಾಡಿಕೊಳ್ಳಿ. ಇನ್ಸ್ತಾಲ್ ಮಾಡಿದ ಮೇಲೆ ಅಪ್ಪ್  ಅನ್ನು ಒಪೆನ್ ಮಾಡಿ.    ಲಿಪಿಕಾ ಆಪ್    ಟಾಸ್ಕ್  ಬಾರ್ ನಲ್ಲಿ 'A' ಎನ್ನುವ ಐಕಾನ್ ಅನ್ನು ಕ್ಲಿಕ್ ಮಾಡಿ. ಲಿಪಿಕಾ ಐಮ್ಈ ಅನ್ನು ಕ್ಲಿಕ್ ಮಾಡಿ. ಕನ್ನಡ ಭಾಷೆಯನ್ನು ಆಯ್ಕೆ ಮಾಡಿ.  ಆ ನಂತರ ಯಾವುದೇ ಮ್ಯಾಕ್ OS ಆಪ್ ನಲ್ಲಿ ನೀವು  ಕನ್ನಡದಲ್ಲಿ ಟೈಪ್ ಮಾಡಬಹುದು.  ಕೇವಲ ಕ್ರೊಮ್ ವೆಬ್ ಬ್ರೌಸರ್ ನಲ್ಲಿ ನಿಮಗೆ ಕನ್ನಡ ಟೈಪ್ ಮಾಡಬೇಕಾದಲ್ಲಿ ಈ ಕೆಳಗಿನ ಲಿಂಕ್ ಅನ್ನು ಕ್ಲಿಕ್ ಮಾಡಿ  ಗೂಗಲ್ ಇನ್ಪುಟ್ ಮೆಥಡ್  (Google Input Method) ಗೂಗಲ್ ಕ್ರೊಮ್ ಎಕ್ಸ್ ಟೆನ್ಷನ್ ಅನ್ನು ಇನ್ಸ್ತಾಲ್ ಮಾಡಿಕೊಳ್ಳಿ. ಅದನ್ನು ಒಪೆನ್ ಮಾಡಿ ಕನ್ನಡ ಭಾಷೆ ಯನ್ನು ಆಡ್ ಮಾಡಿಕೊಳ್ಳಿ.  ವೆಬ್ ಬ್ರೌಸರ್ ನಲ್ಲಿ ನಿಮಗೆ ಕನ್ನಡ ಟೈಪ್ ಮಾಡಬೇಕಾದಲ್ಲಿ ಈ ನೀಲಿ ಕೀ ಬೋರ್ಡ್ ಐಕಾನ್ ಅನ್ನು ಬುಕ್ಮಾರ್ಕ್ ಬಾರ್ ನಲ್ಲಿ ಕ್ಲಿಕ್ ಮಾಡಿ ಕನ್ನಡ ಫೊನೆಟಿಕ್ ಆಪ್ಷನ್ ಅನ್ನು ಆಯ್ಕೆ ಮಾಡಿ.  ಕನ್ನಡ ಟೈಪಿಂಗ್ ಅನ್ನು ಮ್ಯಾಕ್ ಬುಕ್ ಮತ್ತು ವಿನ್ಡೋಸ್ ಲ್ಯಾಪ್ ಟಾಪ್ ಬ್ರೌಸರ್ ನಲ್ಲಿ ಆನಂದಿಸಿ.  ಮೈಕ್ರೊಸಾಫ್ಟ್  ವಿನ್ಡೋಸ್ ನಲ್ಲಿ ಯಾವ ಆಪ್ ನಲ್ಲಿ ಬೇಕಾದರೂ ಕನ್ನಡ ಟೈಪ್ ಮಾಡಲು ನಿಮಗೆ ಎರಡು ಆಯ್ಕೆ ಇವೆ: ೧. ಫೊನೆಟಿಕ್ ಕನ್ನಡ ಟೈಪ್ ಮಾಡಲು