如何从驱动器全名获取驱动器号
如何在 Windows 下使用 C# 从驱动器(例如 WORKMEMORYSTICK)的已知全名(如果存在)获取驱动器号(例如 F:\)?
即使反过来也是一个开始。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
DriveInfo
类公开获取所有可用驱动器的方法(GetDrives
),您可以枚举这些与给定字符串匹配的内容。像下面这样的东西应该可以帮助你到达那里:正如 abatishchev 所提到的,由于我孩子的时间要求,最初没有详细说明,存在一个潜在的问题,因为你只会匹配 first< /em> 具有该标签的驱动器,因此,如果您的系统需要它,则需要在逻辑中考虑这一点(不过,确定两个驱动器中的哪一个是所需的驱动器仅基于非-唯一的字符串并不比猜测更好,或者作为我在下面提到,询问用户(如果这是输入)他们的意思是哪一个。)
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: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.)
您可以循环浏览所有驱动器并检查 DriveInfo 对象的名称和卷标签属性。
请参阅此问题的代码示例(用于获取 USB 驱动器盘符,但您可以轻松调整它):
如何查找 USB 驱动器盘符?
抱歉,我无法为您提供一些代码,但我现在使用的是 Mac :)
You can cycle through all drives and check the Name and VolumeLabel properties of DriveInfo object.
See this question for code examples (used to get the USB drive letter, but you can easily adapt it):
How to find USB drive letter?
Sorry I cannot provide you some code, but I'm on a Mac right now :)