Working but crashing is weird to say, but the hardware status gets printed in the console window but my application crash when i run this. I have it on the load of the form. Error says: Object reference not set to an instance of an object
And here is the code:
// Hardware check
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:
//
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);
}