简体   繁体   中英

Get drive letter from USB via VolumeLabel

I thought this would be simple. I need the path to a USB but since I dont know which letter it will be assigned I thought I just use the volume label which doesnt change. Here my simple code:

 var alldrives = DriveInfo.GetDrives();
 string destd = alldrives.Where(x => x.VolumeLabel.Equals("UB64")).First().Name;

This throws Io exception (device is unavailable) even though the usb is plugged in. Could somebody let me know why please?

In my view this is different from a full list, as I already have a type of address and just need to convert to full path as opposed to start from nothing.

Chris pointed me to the right answer. In case it helps anybody else here it is:

var alldrives = DriveInfo.GetDrives();
string destd="";
foreach (var drvs in alldrives)
{
    try
    {
          if (drvs.VolumeLabel.Equals("UB64"))
              destd = drvs.Name;
              break;
     }
     catch
     {
        continue;
     }
}

ie it is not the USB causing the error...

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