3

My application needs to populate a combobox with the list of Drive Letters that are available for mapping. DriveInfo class gives me the list of all drive names in the sytem. I am wondering if there is an api which exposes the list of drives that are available for mapping.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
battech
  • 803
  • 2
  • 13
  • 25

2 Answers2

4

Following piece will give you list of available drive letters for mapping

Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char)i +":")
          .Except(DriveInfo.GetDrives().Select(s=>s.Name.Replace("\\","")))

Below piece returns list of used drives.

DriveInfo.GetDrives().Select(s=>s.Name.Replace("\\",""))  
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

Given that (usable) drive letters are simply all the letters in the alphabet, could you not just do something like this:

    public char[] getAvailableDriveLetters()
    {
        List<char> availableDriveLetters = new List<char>() { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };

        DriveInfo[] drives = DriveInfo.GetDrives();

        for (int i = 0; i < drives.Length; i++)
        {
            availableDriveLetters.Remove((drives[i].Name).ToLower()[0]);
        }

        return availableDriveLetters.ToArray();
    }

Then the availableDriveLetters variable will contain the remaining letters to use for your mapped drive - you could also exclude commonly used letters (e.g. - 'a', 'b','c', 'd') if you wanted when initializing the list of available letters.

OBR_EXO
  • 600
  • 4
  • 19