简体   繁体   中英

Check Drive Exists(string path)

How to check the drive is exists in the system from the given string in WPF. I have tried the following

Ex: FileLocation.Text = "K:\\TestDrive\\XXX";

if (!Directory.Exists(FileLocation.Text))
{
         MessageBox.Show("Invalid Directory", "Error", MessageBoxButton.OK);
         return;
}

It is checking full path but it should check "K:\\" from the text . Could you please guide me

EDIT 1: " K:\\TestDrive\\XXX " is not static

EDIT 2: I tried the below, in my system i'm having 3 drives C, D and E but it showing false .

Environment.SystemDirectory.Contains("D").ToString(); = "False"
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.....

You can use Environment.GetLogicalDrives() to obtain an string[] of logical drives in your system.

var drive = Path.GetPathRoot(FileLocation.Text);
if (Environment.GetLogicalDrives().Contains(drive, StringComparer.InvariantCultureIgnoreCase))
{
         MessageBox.Show("Invalid Directory", "Error", MessageBoxButton.OK);
         return;
}

你可以试试这个....

MessageBox.Show(Environment.SystemDirectory.Contains("D").ToString());

I suppose this depends on what exactly you are hoping to accomplish. If you are trying to iterate through the drives and test to make sure each drive exists, then Environment.GetLogicalDrives() or DriveInfo.GetDrives() is appropriate as it allows you to iterate through the drives.

However, if all you care about is testing to see if ONE drive exists for a particular path, getting the entire list of drives to check if it is contained is a bit backwards. You would want to use Directory.Exists() as it just checks if that single path exists.

bool DriveExists(string fileLocation) {
    string drive = Path.GetPathRoot(fileLocation); // To ensure we are just testing the root directory.

    return Directory.Exists(drive); // Does the path exist?
}

You can check drives in C# like this

   foreach (var drive in DriveInfo.GetDrives())
   {
       //Code goes here
   }

如果任何驱动器的字母是 E,这将得到。您可以将其更改为任何其他字母。

DriveInfo.GetDrives().Any(d => d.Name.ToUpper()[0] == 'E');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM