Check Drive Exists(string path)

string drive = Path.GetPathRoot(FileLocation.Text);   // e.g. K:\

if (!Directory.Exists(drive))
{
     MessageBox.Show("Drive " + drive + " not found or inaccessible", 
                     "Error", MessageBoxButton.OK);
     return;
}

Of course, additional sanity checks (does the path root have at least three characters, is the second one a colon) should be added, but this will be left as an exercise to the reader.


you can do follow

bool isDriveExists(string driveLetterWithColonAndSlash)
{
    return DriveInfo.GetDrives().Any(x => x.Name == driveLetterWithColonAndSlash);
}

This is Because Environment.SystemDirectory.XXXXX is all about where the system/windows is installed ...... not for whole HD.

for this you can use.....

    foreach (var item in System.IO.DriveInfo.GetDrives())
    {
        MessageBox.Show(item.ToString());
    }

it will show all drives including USBs that are attached.....

Tags:

C#

Wpf