C# DriveInfo.GetDrives () slow on select computer

1.6k views Asked by At

I use the DriveInfo.GetDrives() method in my code to populate a combobox with all available and ready removable drives on my specified computer. It works great on three test computers, but on a single computer when the user clicks the button that opens the window with the combo box in it (and the GetDrives in the constructor) it takes a good few seconds before the window opens up.

The computer is running Windows 7 and the only things to note would be it has a RAID setup.

Once open it is responsive it just hangs when opening for some reason. I couldn't find anything of help on the MSDN documentation and I found no similar cases online. Please let me know if you have had any experiences with similar issues or any suggestions.

I extracted the window that used the DriveInfo from my project and built a test application. The code behind is below:

public partial class MainWindow : Window
{
    //Instance variables used in class and refrenced in 'get' methods
    int count;
    string[] driveNames;

    public MainWindow() //Constructor
    {
        InitializeComponent();
        getInfo(); //Populate instance vars
    }

    public string[] getRemovableDrives() //Returns array of drive letters for removable drives in  computer
    {
        return driveNames;
    }

    public int getRemovableDrivesCount() //Returns number of removable drives in computer
    {
        return count;
    }

    private void getInfo() //Run once to get information about removable drives on computer and store into instance vars
    {
        count = 0;
        List<string> drivesTemp = new List<string>();

        foreach (DriveInfo d in DriveInfo.GetDrives())
        {
            if (d.IsReady == true && d.DriveType == DriveType.Removable && d.DriveFormat == "FAT32")
            {
                drives.Items.Add(d.VolumeLabel + " (" + d.Name + ")");
                drivesTemp.Add(d.Name);
                count++;
            }
        }

        driveNames = new string[count];
        for (int i = 0; i < count; i++)
        {
            driveNames[i] = drivesTemp[i];
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e) //Selects first available drive in drop down box
    {
        drives.SelectedIndex = drives.Items.Count - 1;
    }

    private void format_Click(object sender, RoutedEventArgs e) //Attempts to format drive
    {
        string drive = driveNames[drives.SelectedIndex];

        try
        {
            Directory.CreateDirectory(drive + "LOOKOUT.SD");
            Directory.CreateDirectory(drive + "LOOKOUT.SD\\CONFIG");
            Directory.CreateDirectory(drive + "LOOKOUT.SD\\HISTORY");
            Directory.CreateDirectory(drive + "LOOKOUT.SD\\TEST");
            Directory.CreateDirectory(drive + "LOOKOUT.SD\\UPDATES");
            Directory.CreateDirectory(drive + "LOOKOUT.SD\\VPROMPTS");

            MessageBox.Show("Format complete, your removable device is now ready to use.", "Format Successful", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        catch
        {
            MessageBox.Show("Your removable device has failed to format correctly.", "Format Failure", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }

        Close();
    }

    private void cancel_Click(object sender, RoutedEventArgs e) //Closes window without formatting
    {
        Close();
    }
}
1

There are 1 answers

2
Tim Jarvis On

It's highly likely that one of the drives in the problem machine is in an inactive mode and takes a few seconds to spin up. (I have this same issue on my home machine)