[C#] 장치관리자 목록 추출하기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Management.Instrumentation;
/**
* //KBW : https://powershell.one/wmi/root/cimv2/win32_pnpentity
* {
1 {'Other'}
2 {'Unknown'}
3 {'Running/Full Power'}
4 {'Warning'}
5 {'In Test'}
6 {'Not Applicable'}
7 {'Power Off'}
8 {'Off Line'}
9 {'Off Duty'}
10 {'Degraded'}
11 {'Not Installed'}
12 {'Install Error'}
13 {'Power Save - Unknown'}
14 {'Power Save - Low Power Mode'}
15 {'Power Save - Standby'}
16 {'Power Cycle'}
17 {'Power Save - Warning'}
18 {'Paused'}
19 {'Not Ready'}
20 {'Not Configured'}
21 {'Quiesced'}
}
*
**/
namespace DeviceStatus
{
class Program
{
static void Main(string[] args)
{
String result = "OK";
// 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");
//new ManagementObjectSearcher("Select * from Win32_PnPEntity where Availability = 12 or Availability = 11");
new ManagementObjectSearcher("Select * from Win32_PnPEntity where Availability = 2");
// Any results? There should be!
if (deviceList != null)
// Enumerate the devices
foreach (ManagementObject device in deviceList.Get())
{
// To make the example more simple,
object n = device.GetPropertyValue("Name");
string name = "NoName".ToString();
if (n != null) name = n.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
// Part II, Evaluate the device status.
bool working = ((status == "OK") || (status == "Degraded")
|| (status == "Pred Fail"));
if (working == true)
{
Console.WriteLine("Device name: {0}", name);
Console.WriteLine("\tStatus: {0}", status);
Console.WriteLine("\tWorking?: {0}", working);
}
else if (name == "NoName")
{
Console.WriteLine("Device name: {0}", name);
Console.WriteLine("\tStatus: {0}", status);
Console.WriteLine("\tWorking?: {0}", working);
}
result = "ERROR";
}
//Console.WriteLine("Press any key...");
//Console.ReadKey();
Console.WriteLine("{0}",result);
}
}
}