如何使用 C/C++ 检索可移动存储盘符

How to retrieve removable storage drive letter using C/C++

如何获取可移动驱动器盘符,以便创建路径并将数据从 PC 复制到可移动驱动器?

目前我正在使用

_getcwd(buff,b_SIZE);
MessageBox(buff);
strncpy(Root,buff,3);

如果我直接从 USB 运行 我的软件,我没有问题,它会 returns 给我 D: 或 E:,这取决于我猜的可用性。但我的问题是我必须从 PC 运行 我的 USB 程序,这当然是安装在 C 驱动器中的 PC 程序,因此这个 _getcwd 会给我 C:\ 盘符。这就是为什么我正在寻找是否有一种方法来检查当前的可移动驱动器号。

这是我能找到的最接近我的问题的答案:Detect removable drive (e.g. USB flash drive) C/C++

GetLogicalDrives() will give you all the drives that are currently available. Loop through given bit-vector (bit 0 is drive A:, bit 1 is drive B:, etc.) and for each available drive check if GetDriveType() returns DRIVE_REMOVABLE.

或者,您可以跳过 GetLogicalDrives(),循环遍历所有 26 个字母表,寻找 DRIVE_REMOVABLE