简体   繁体   English

如何从驱动器号获取驱动器名称?

[英]How to get drive name from drive letter?

我已经搜索了互联网,但找不到答案,如何从Windows的c ++的驱动器号中获取驱动器名称?我的意思是如果我说G:\\,它必须给我笔驱动器的名称。例如:可移动磁盘。

It is as simple as calling the GetVolumeInformation API function . 就像调用GetVolumeInformation API函数一样简单。 You pass in the drive letter as the path name ( eg , G:\\ ), and the function fills a buffer containing the volume name (along with other information, if you are interested in any of that). 传递驱动器号作为路径名( 例如 G:\\ ),该函数将填充一个包含卷名的缓冲区(如果您对此感兴趣,则还有其他信息)。

Here is the code required to retrieve the volume name for the G:\\ drive. 这是检索G:\\驱动器的卷名所需的代码。 The volume name is placed into the szVolumeName buffer: 卷名放置在szVolumeName缓冲区中:

WCHAR szVolumeName[MAX_PATH];
BOOL bSucceeded = GetVolumeInformationW(L"G:\\",
                                        szVolumeName,
                                        MAX_PATH,
                                        NULL,
                                        NULL,
                                        NULL,
                                        NULL,
                                        0);

If you want any of the other information while you're calling the function, like the volume's DOS serial number, the file system name, etc., then you can change the parameters from NULL to the appropriate buffers. 如果在调用函数时还需要其他信息,例如卷的DOS序列号,文件系统名称等,则可以将参数从NULL更改为适当的缓冲区。

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

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