如何从驱动器的全名获取驱动器盘符 [英] How to get the drive letter from a full name of the drive

查看:201
本文介绍了如何从驱动器的全名获取驱动器盘符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何可以在驱动器盘符(如F:\),可从已知的全名获得(如果是present)驱动器(如WORKMEMORYSTICK)使用C#在Windows下的

How could the drive letter (e.g. F:\) be obtained from a known full name (if it is present) of a drive (e.g. WORKMEMORYSTICK) using C# under Windows?

即使周围的其他方法将是一个开始。

Even the other way around would be a start.

推荐答案

的的 DriveInfo 类公开的方法来获取所有可用的驱动器(的 GetDrives ),你可以enyumerate这些的比赛你指定的字符串。像下面这样应该可以帮助让你有:

The DriveInfo class exposes a method to get all available drives (GetDrives), you could enyumerate these an match your given string. Something like the following ought to help get you there:

DirectoryInfo root;
var drives = DriveInfo.GetDrives();
foreach (var drive in drives)
{
    if (drive.VolumeLabel == label)
    {
        root = drive.RootDirectory;
        break;
    }
}

正如刚才abatishchev,而不是一开始给我的孩子们的时间要求,阐述了因,存在这样一个潜在的问题,你只打算在第一的驱动器,有一个标签相匹配,因此,你需要考虑到这一点在你的逻辑,如果你的系统需要它(虽然,确定其中的两个驱动器是的的基础上只是一个非唯一的字符串所需的驱动器只不过是一个更好的猜想,或者我提到下面,询问用户(如果这是输入),他们的意思是哪一个。)

As mentioned by abatishchev, and not initially elaborated on due to the time requirements of my children, there is a potential problem in that you're only going to match the first drive which has that label, therefore you will need to account for this in your logic if your system requires it (though, determining which of two drives is the desired drive based on nothing but a non-unique string is no better than a guess, or as I mention below, asking the user (if this is input) which one they meant.)

这篇关于如何从驱动器的全名获取驱动器盘符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆