Search code examples
c#listcomboboxdrive

Exception to ComboBox


I am looking for a way to remove the physical disk 0 when the aquisition of the list of device on the computer.

the command executed is as follows:

ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
        foreach (ManagementObject moDisk in mosDisks.Get())
        {
            driveList.Items.Add(moDisk["Model"].ToString());
        }

thank you for your help.


Solution

  • ManagementObjectSearcher mosDisks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
        foreach (ManagementObject moDisk in mosDisks.Get())
        {
            if(!moDisk["DeviceId"].ToString().Contains("PHYSICALDRIVE0"))
            {
                driveList.Items.Add(moDisk["Model"].ToString());
            }
        }
    

    Or more simply, change your WQL to this:

    SELECT * FROM Win32_DiskDrive WHERE NOT NAME LIKE '%PHYSICALDRIVE0'