简体   繁体   English

如何在C ++中将驱动器号转换为物理驱动器名称?

[英]How do I convert drive Letter to a physical drive name in c++?

My question is this: Given an offline disk connected to my windows machine, how can I get it's volume letter? 我的问题是这样的:给定一个连接到Windows计算机的脱机磁盘,如何获得它的大写字母?

The scenario: I need to create a handler to a shared storage (with CreateFile function) in order to use it in the DeviceIoControl function (need to perform SCSI reservations). 场景:我需要为共享存储创建一个处理程序(使用CreateFile函数),以便在DeviceIoControl函数中使用它(需要执行SCSI保留)。

The "CreateFile" function gets the physical drive name (ie \\.\\PhysicalDisk1) or the volume letter of the disk (ie \\.\\E:). “ CreateFile”功能获取物理驱动器名称(即\\。\\ PhysicalDisk1)或磁盘的卷号(即\\。\\ E :)。

My problem is, that I don't want to use the physical drive name, just the disk letter. 我的问题是,我不想使用物理驱动器名称,而只使用磁盘字母。 This works fine when the disk is online, but I have to support also when the disk is offline. 当磁盘处于联机状态时,这可以正常工作,但是当磁盘处于脱机状态时,我也必须提供支持。

Is there a way to determine the letter when the device/disk is offline? 设备/磁盘处于脱机状态时,是否可以确定字母? I assume there is, because when I get the disk online, windows automatically gives it the right letter. 我认为是有原因的,因为当我使磁盘联机时,Windows会自动给它正确的字母。 So if the operation system knows what will be the letter, there must be an API or a tool that I can use too. 因此,如果操作系统知道字母是什么,那么也必须有一个我也可以使用的API或工具。

Thanks, Daniel. 谢谢,丹尼尔。

Unfortunately, when a drive is offline the drive letter is available for use by another drive that may be inserted. 不幸的是,当驱动器离线时,该驱动器号可被可能插入的另一个驱动器使用。 So although Windows remembers what drive letter a particular drive used to have, and it attempts to re-use the same letter when it is re-inserted, if another drive was inserted that used the remembered letter then the old drive will get a different letter if it is then inserted. 因此,尽管Windows会记住特定驱动器曾经拥有的驱动器号,并且在重新插入时它会尝试重复使用相同的驱动器号,但是如果插入了另一个驱动器,该驱动器号使用了已记住的字母,则旧驱动器将获得不同的字母如果已插入。

But you cannot open a Physical Drive, either by name or letter, if that drive is not currently connected. 但是,如果当前未连接,则无法按名称或字母打开物理驱动器。 You'll simply get the equivalent of "File not found". 您将仅获得“找不到文件”的等效内容。

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

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