C# Get the Next Available Drive Letter

Probably some neater ways to do this, but this is fairly readable so is what I use;

private static string GetNextDriveLetter()
{
    List<string> InUse = new List<string>();
    
    DriveInfo[] drives = DriveInfo.GetDrives();
        foreach(DriveInfo drive in drives)
            InUse.Add(drive.Name.Substring(0, 1).ToUpper());
    
    char[] alphas = "EFGHIJKLMNOPQRSTUVWXY".ToCharArray();
    
    foreach(char alpha in alphas)
        if (!InUse.Contains(alpha.ToString())) return alpha.ToString();
    
    return null;
}

You may have noticed I have skipped some of the alphabet, I don’t like mapping drives to A, B, C, or D even if available, and I always try and keep Z empty as well.

Leave a Reply