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

Query hardware device status in C#

Rate me:
Please Sign up or sign in to vote.
4.25/5 (12 votes)
9 Oct 2008CPOL1 min read 77.6K   2.2K   26   6
How to query the status of a hardware device in .NET using C#.

DeviceStatus

Introduction

To put it simple: if you want to query hardware device status in .NET, the ManagementObjectSearcher is your friend. Using it properly is a bit difficult, but it's not that bad.

Background

My task was to create a hardware device health monitor service for a high availability server that runs an important .NET application. Since the environment already had .NET framework installed, I decided to write the health monitor in C#, and that’s where hell broke loose.

I spent a day searching the Internet for a native .NET way to query the hardware status, with no avail. At the end of the day, I found the ManagementObjectSearcher class, but I didn’t have any clues on the valid search terms. After a long search session, I bumped into a Chinese page (yes, I was that desperate) that showed a list of valid terms and a truckload of Chinese characters.

I wrote a small program that listed all the ManagementObjects in all the valid ManagementObjectSearchers, and bingo! The winner was contestant number 135: Win32_PnPEntity. With this in my hand, I could Google for the valid attributes and create the following sample.

Using the code

Our code is a simple console application that lists all the available devices and tells whether it's working according to the device status. To use it, you have to reference the System.Management component.

So, the actual code:

C#
// Query the device list trough the WMI. If you want to get
// all the properties listen in the MSDN article mentioned
// below, use "select * from Win32_PnPEntity" instead!
ManagementObjectSearcher deviceList =
    new ManagementObjectSearcher("Select Name, Status from Win32_PnPEntity");

// Any results? There should be!
if ( deviceList != null )
    // Enumerate the devices
    foreach ( ManagementObject device in deviceList.Get() )
    {
        // To make the example more simple,
        string name = device.GetPropertyValue("Name").ToString();
        string status = device.GetPropertyValue("Status").ToString();

        // Uncomment these lines and use the "select * query" if you 
        // want a VERY verbose list
        // foreach (PropertyData prop in device.Properties)
        //    Console.WriteLine( "\t" + prop.Name + ": " + prop.Value);

        // More details on the valid properties:
        // http://msdn.microsoft.com/en-us/library/aa394353(VS.85).aspx
        Console.WriteLine( "Device name: {0}", name );  
        Console.WriteLine( "\tStatus: {0}", status );

        // Part II, Evaluate the device status.
        bool working = (( status == "OK" ) || ( status == "Degraded" )
            || ( status == "Pred Fail" ));

        Console.WriteLine( "\tWorking?: {0}", working );
    }

Points of interest

An advice in advance: if you have to learn anything about the local machine status, try to dig up a WMI namespace for that; looking at the log my first dumping application created, even the question for 42 is there. The only trick is to find the correct query :)

License

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


Written By
Hungary Hungary
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to disable the hardware? Pin
Member 102883935-Jun-15 0:58
Member 102883935-Jun-15 0:58 
GeneralAnother Another EXCELLENT Pin
MemberGrasshopper21-Jan-10 9:34
MemberGrasshopper21-Jan-10 9:34 
Thank you for solving my problem. Thanks to this example I am able to get exact status of my Twain scanner. I am back in business thanks to this example. THANK YOU. This is why I LOVE being an engineer - people take pride to share what they had figured out, not trying to hide it. THANKS AGAIN.
GeneralAnother Excellent... Pin
spl29-Oct-08 21:12
spl29-Oct-08 21:12 
GeneralExcellent Pin
merlin98110-Oct-08 4:06
professionalmerlin98110-Oct-08 4:06 
GeneralWMI Code Generator Pin
Günther M. FOIDL9-Oct-08 13:20
Günther M. FOIDL9-Oct-08 13:20 
GeneralRe: WMI Code Generator Pin
szutyok9-Oct-08 14:15
szutyok9-Oct-08 14:15 

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.