Created
March 13, 2017 18:39
-
-
Save kflu/061e4d02a62e9dcf8ddb018068d5f0f5 to your computer and use it in GitHub Desktop.
List hardware devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Management; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ListDevices(); | |
} | |
static void ListDevices() | |
{ | |
// 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); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment