我正在使用WMI查找计算机上的所有Win32_DiskDrive
。我想要排除临时安装的驱动器,例如USB驱动器。
有办法做到这一点吗?
答案 0 :(得分:1)
您可以使用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);
}
}