title | author | date | CreateTime | categories |
C# 獲得裝置usb資訊 | lindexi | 2018-2-13 17:23:3 +0800 | 2018-2-13 17:23:3 +0800 | C# |
本文告訴大家如何獲得裝置的usb來進行判斷是否有哪些usb和找不到usb可能是什麼。
需要在項目右擊引用,點選程式集,搜尋 System.Management 然後安裝他
然後使用下面的代碼就可以獲得裝置的 usb 請看代碼
static List<(string DeviceID, string PNPDeviceID, string Description)> GetUSBDevices()
{
List<(string DeviceID, string PNPDeviceID, string Description)> devices = new List<(string, string, string)>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
{
collection = searcher.Get();
}
foreach (var device in collection)
{
devices.Add(((string) device.GetPropertyValue("DeviceID"),
(string) device.GetPropertyValue("PNPDeviceID"),
(string) device.GetPropertyValue("Description")));
}
collection.Dispose();
return devices;
}
如果需要判斷是否存在某個 usb ,就通過 pid vid 判斷,判斷的方法是拿
PNPDeviceID
字元串比較