[英]How to get the drive letter of USB device using WMI
我需要跟蹤 C# 應用程序中的 USB 插入和移除事件,因此我根據 SO 上的其他問題提出了以下想法。
我不能用這個方法
var drives = DriveInfo.GetDrives()
.Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable).ToList();
因為當您需要區分連接的設備和斷開連接的設備(新設備可以具有與以前緩存的設備相同的驅動器號和名稱時),這會導致很多麻煩。
所以我決定使用WMI來解決這個問題,但是我發現無法通過Win32_USBHub
類獲取指定USB設備的驅動器號。 然后我想我可以像這樣執行另一個查詢
foreach (ManagementObject device in new ManagementObjectSearcher(@"SELECT * FROM Win32_USBHub").Get())
{
string deviceID = (string)device.GetPropertyValue("DeviceID");
Console.WriteLine("{0}", deviceID);
string query = string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID='{0}'", deviceID);
foreach (ManagementObject o in new ManagementObjectSearcher(query).Get())
{
string name = (string)o.GetPropertyValue("Name");
Console.WriteLine("{0}", name);
}
Console.WriteLine("==================================");
}
但它根本不起作用——每次當我嘗試執行與Win32_LogicalDisk
表一起使用的查詢時,我都會收到異常“無效查詢”。
為什么? 我究竟做錯了什么? 我該如何解決? 也許有更好的方法來解決這個問題?
提前致謝。
您會收到異常,因為您的deviceID
包含需要轉義的字符(反斜杠)。 使用簡單的替換,你不應該得到例外。
string query = string.Format("SELECT * FROM Win32_LogicalDisk WHERE DeviceID='{0}'", deviceID.Replace(@"\", @"\\"));
但是,從 WMI 獲取 USB 驅動器盤符要復雜一些。 您需要學習一些課程,如@MSalters 在他的評論中發布的鏈接中所述:
Win32_DiskDrive-> Win32_DiskDriveToDiskPartition -> Win32_DiskPartition -> Win32_LogicalDiskToPartition -> Win32_LogicalDisk.
在這里找到的一些修改過的代碼版本對我有用:
foreach (ManagementObject device in new ManagementObjectSearcher(@"SELECT * FROM Win32_DiskDrive WHERE InterfaceType LIKE 'USB%'").Get())
{
Console.WriteLine((string)device.GetPropertyValue("DeviceID"));
Console.WriteLine((string)device.GetPropertyValue("PNPDeviceID"));
foreach (ManagementObject partition in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + device.Properties["DeviceID"].Value
+ "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
{
foreach (ManagementObject disk in new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID='"
+ partition["DeviceID"]
+ "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
{
Console.WriteLine("Drive letter " + disk["Name"]);
}
}
}
這是 Michalk 對 C# 8 的回答的重構版本。
我將它與此問題的答案結合使用, 使用 Windows 服務和 c# 檢測 USB 驅動器插入和刪除,相關問題
static IEnumerable<(string deviceId, string pnpDeviceId, string driveLetter)> SelectDeviceInformation()
{
foreach (ManagementObject device in SelectDevices())
{
var deviceId = (string)device.GetPropertyValue("DeviceID");
var pnpDeviceId = (string)device.GetPropertyValue("PNPDeviceID");
var driveLetter = (string)SelectPartitions(device).SelectMany(SelectDisks).Select(disk => disk["Name"]).Single();
yield return (deviceId, pnpDeviceId, driveLetter);
}
static IEnumerable<ManagementObject> SelectDevices() => new ManagementObjectSearcher(
@"SELECT * FROM Win32_DiskDrive WHERE InterfaceType LIKE 'USB%'").Get()
.Cast<ManagementObject>();
static IEnumerable<ManagementObject> SelectPartitions(ManagementObject device) => new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskDrive.DeviceID=" +
"'" + device.Properties["DeviceID"].Value + "'} " +
"WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get()
.Cast<ManagementObject>();
static IEnumerable<ManagementObject> SelectDisks(ManagementObject partition) => new ManagementObjectSearcher(
"ASSOCIATORS OF {Win32_DiskPartition.DeviceID=" +
"'" + partition["DeviceID"] + "'" +
"} WHERE AssocClass = Win32_LogicalDiskToPartition").Get()
.Cast<ManagementObject>();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.