using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Collections;
namespace ConsoleApplication29
{
class Program
{
private static void GatherUsbInformation()
{
var mos = new ManagementObjectSearcher("select DeviceID from Win32_PnPEntity");
var mbos = new ArrayList(mos.Get());
var data = new Dictionary<string, string[]>();
for (var i = 0; i < mbos.Count; i++)
{
var managementBaseObject = mbos[i] as ManagementBaseObject;
if (managementBaseObject == null)
{
continue;
}
var deviceId = managementBaseObject.Properties["DeviceID"].Value as string;
if (deviceId == null || !deviceId.StartsWith("USB"))
{
continue;
}
if (!data.ContainsKey(deviceId))
{
data.Add(deviceId, new string[8]);
}
else if (data.ContainsKey(deviceId))
{
continue;
}
var mo = managementBaseObject as ManagementObject;
var inParams = mo.GetMethodParameters("GetDeviceProperties");
var result = mo.InvokeMethod(
"GetDeviceProperties",
inParams,
new InvokeMethodOptions()
);
if (result?.Properties["deviceProperties"].Value == null)
{
continue;
}
foreach (var deviceProperties in result.Properties["deviceProperties"].Value as ManagementBaseObject[])
{
var keyName = deviceProperties.Properties["KeyName"].Value as string;
var value = deviceProperties.Properties["Data"].Value as string;
if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(keyName))
{
//MachineInformationGatherer.Logger.LogTrace(
// $"KeyName {keyName} or Value {value} was null or whitespace for device ID {deviceId}");
continue;
}
Console.WriteLine(keyName);
Console.WriteLine(value);
switch (keyName)
{
case "DEVPKEY_Device_BusReportedDeviceDesc":
{
data[deviceId][0] = value;
break;
}
case "DEVPKEY_Device_DriverDesc":
{
data[deviceId][1] = value;
break;
}
case "DEVPKEY_Device_DriverVersion":
{
data[deviceId][2] = value;
break;
}
case "DEVPKEY_Device_DriverDate":
{
var year = int.Parse(value.Substring(0, 4));
var month = int.Parse(value.Substring(4, 2));
var day = int.Parse(value.Substring(6, 2));
var hour = int.Parse(value.Substring(8, 2));
var minute = int.Parse(value.Substring(10, 2));
var second = int.Parse(value.Substring(12, 2));
data[deviceId][3] =
new DateTime(year, month, day, hour, minute, second).ToString();
break;
}
case "DEVPKEY_Device_Class":
{
data[deviceId][4] = value;
break;
}
case "DEVPKEY_Device_DriverProvider":
{
data[deviceId][5] = value;
break;
}
case "DEVPKEY_NAME":
{
data[deviceId][6] = value;
break;
}
case "DEVPKEY_Device_Manufacturer":
{
data[deviceId][7] = value;
break;
}
case "DEVPKEY_Device_Children":
{
var children = deviceProperties.Properties["DEVPKEY_Device_Children"];
if (children.Value != null)
{
if (children.IsArray)
{
foreach (var child in children.Value as string[])
{
mos.Query = new ObjectQuery(
$"select * from Win32_PnPEntity where DeviceID = {child}");
var childs = mos.Get();
foreach (var child1 in childs)
{
mbos.Add(child1);
}
}
}
}
break;
}
}
}
}
}
static void Main(string[] args)
{
GatherUsbInformation();
Console.ReadKey();
}
}
}
运行结果:
代码来自 https://github.com/L3tum/HardwareInformation/blob/master/HardwareInformation/Providers/WindowsInformationProvider.cs