使用WMI查找永久安装的物理驱动器

时间:2013-04-30 01:58:48

标签: windows wmi

我正在使用WMI查找计算机上的所有Win32_DiskDrive。我想要排除临时安装的驱动器,例如USB驱动器。

有办法做到这一点吗?

1 个答案:

答案 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);
            }
        }