Search code examples
c#.netwindowsdriveremovable-storage

C# code showing multiple removeable drives window 8


Below is the C# code that I'm using to get all the removeable drives and then populate the combobox with this list:

comboBox1.DataSource = DriveInfo.GetDrives()
                .Where(drive => drive.DriveType == DriveType.Removable).ToList();

The code works perfectly but with one exception, it shows two removable drives in the combo box while I've attached only one.

Is there any way that I can get only one i.e. the one I've attached at the USB port. I'm using Windows 8 OS.


Solution

  • Use drive.IsReady

    Hopes you are making Removal Drive Security!!!!

    Try this

     comboBox1.DataSource = DriveInfo.GetDrives()
                .Where(drive => drive.DriveType == DriveType.Removable && drive.IsReady).ToList()
    

    thanks