简体   繁体   English

通过VolumeLabel从USB获取驱动器号

[英]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. 我需要USB路径,但由于我不知道它将分配给哪个字母,我想我只是使用不会改变的卷标。 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? 即使插入了USB,也会引发Io异常(设备不可用)。有人可以让我知道为什么吗?

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... 即不是导致错误的USB ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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