如何使用 C/C++ 检索可移动存储驱动器号 [英] How to retrieve removable storage drive letter using C/C++

查看:31
本文介绍了如何使用 C/C++ 检索可移动存储驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I get the removable drive letter so that I could create a path and copy data from PC to the removable drive?

目前我正在使用

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

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

I have no problem if I run my software directly from the USB and it will returns to me either D: or E:, which depends on the availability I guess. But my problem is I have to run my USB program from the PC which is of course the PC's program installed in C drive, and therefore this _getcwd will give me C:\ drive letter. That's why I'm looking for if there's a way to check the current removable drive letter instead.

这是我能找到的最近的问题:检测可移动驱动器(例如 U 盘)C/C++

This is the nearest I could find for my question: Detect removable drive (e.g. USB flash drive) C/C++

推荐答案

GetLogicalDrives() 将为您提供当前可用的所有驱动器.循环遍历给定的位向量(位 0 是驱动器 A:,位 1 是驱动器 B: 等)并检查每个可用驱动器是否 GetDriveType() 返回 DRIVE_REMOVABLE.

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.

Or, you can just skip GetLogicalDrives(), and loop through all 26 letters of the alphabet, looking for DRIVE_REMOVABLE.

这篇关于如何使用 C/C++ 检索可移动存储驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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