0
我試圖找到一個特定的USB設備(1或更多)連接到我的電腦,並檢索相關的路徑到裝入的驅動器。理想情況下,它將通過查找USB設備的VID/PID,但我不確定如何執行此操作。以下工作,但必須有一些方法來獲取單個查詢中的數據。管理對象查詢Windows(C#)來查找USB設備
我在做什麼這裏正在或具有模型匹配HS SD Card Bridge USB Device
,並找到相關的物理驅動器#並用它來找到安裝分區的物理驅動器..
foreach (ManagementObject disk in disks.Get()) {
//look for drives that match our string
Match m = Regex.Match(disk["model"].ToString(), "HS SD Card Bridge USB Device");
if (m.Success) {
m = Regex.Match(disk["DeviceID"].ToString(), @"PHYSICALDRIVE(\d+)");
if (m.Success) {
int driveNumber = Int32.Parse(m.Groups[1].ToString());
ManagementObjectSearcher mapping = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
foreach (ManagementObject map in mapping.Get()) {
m = Regex.Match(map["Antecedent"].ToString(), @"Disk #" + driveNumber + ",");
if (m.Success) {
string drive = map["Dependent"].ToString();
m = Regex.Match(drive, @"([A-Z]):");
if (m.Success) {
drive = m.Groups[1].ToString(); //< -- **FOUND**
}
}
}
//USBDevice dev = new USBDevice("", "");
// list.Items.Add();
Console.WriteLine("");
}
}
}
是有辦法要從VID/PID做到這一點,並構建搜索查詢的方式,因此它只需要一個查詢?