我正在使用 WMI 来查找Win32_DiskDrive
机器上的所有 s。我想排除临时安装的驱动器,例如 USB 驱动器。
有没有办法做到这一点?
您可以像这样使用 WMI 搜索 USB 设备:
public void CollectUSBDevices()
{
NameValueCollection collection = new NameValueCollection();
ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("SELECT * FROM win32_pnpentity where deviceid like 'USB%'");
// Iterate through all found USB objects.
foreach (ManagementObject dm in searcher2.Get())
{
string nameValue = dm["Name"].ToString();
string devid = dm["DeviceID"].ToString();
if (nameValue.Contains("Generic USB Hub") || nameValue.Contains("USB Root Hub"))
continue;
if (nameValue.Contains("USB Mass Storage Device") || devid.Contains("USBSTOR\\"))
collection.Add("USBDevice", nameValue);
}
}