How to retrieve removable storage drive letter usi

2019-08-10 02:15发布

This question already has an answer here:

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

Currently I'm using

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

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.

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

1条回答
Emotional °昔
2楼-- · 2019-08-10 02:21

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.

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

查看更多
登录 后发表回答