0

I have a ComboBox named DriveDropDown which I would like to display all removable drives. This is updated when the Refresh button is clicked. However, when I click the refresh button, nothing shows up, even though my external hard disk is connected over USB. Any ideas? Code is below.

private void RefreshButton_Click(object sender, EventArgs e)
{
    DriveDropDown.Items.Remove("Press refresh to query available drives.");
    DriveInfo[] allDrives = DriveInfo.GetDrives();

    foreach (DriveInfo d in allDrives)
    {
        if (d.IsReady == true)
        {
            if (d.DriveType == DriveType.Removable)
            {
                DriveDropDown.Items.Add(d.Name);
            }
        }
        else
        {
            MessageBox.Show("An error occurred querying available devices.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }           
    }
}
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
imrolii
  • 1
  • 3
  • [Tutorial: Learn to debug C# code using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger?view=vs-2019). This allows you to inspect the variables and properties and you will see why the code doesn't execute `DriveDropDown.Items.Add(d.Name);`. – Olivier Jacot-Descombes Feb 13 '21 at 16:24
  • Remove the `d.IsReady == true` condition. Note that not all USB devices are installed as Removable resources; some USB drives used for storage are set as `Fixed` even though connected via an external USB connector. – Jimi Feb 13 '21 at 16:27
  • @OlivierJacot-Descombes I've debugged the program multiple times (f5), no issues are thrown. – imrolii Feb 13 '21 at 16:28
  • @Jimi thanks for your response: I've attempted removing that condition and I get the same issue, hence why I added it. About the fixed drive situation, do you know any such solutions as to this? – imrolii Feb 13 '21 at 16:30
  • This will allow you to inspect the values of `allDrives.Length` (does the number match what you expect?), `d.IsReady` and `d.DriveType`. Does the USB have the values you expected? – Olivier Jacot-Descombes Feb 13 '21 at 16:31
  • 1
    [Get the serial number of USB storage devices in .Net Core 2.1](https://stackoverflow.com/a/51806262/7444103). This gives you all that's available (tagged as .Net Core 2.1, but it's the same exact thing with .Net Framework). The *title* is misleading here, that code gets you way more than just the *serial number*. Probably overshooting for what you need, but you can strip it down to the bare essential. – Jimi Feb 13 '21 at 16:37
  • Thanks @Jimi and everybody else, I'm going to try the serial number solution. Will update this accordingly. – imrolii Feb 14 '21 at 09:51

0 Answers0