How to retrieve removable storage drive letter using C/C++ [duplicate]

蹲街弑〆低调 提交于 2019-12-24 12:52:44

问题


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:


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.



来源:https://stackoverflow.com/questions/30434769/how-to-retrieve-removable-storage-drive-letter-using-c-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!