1

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++

Community
  • 1
  • 1
KevinYong
  • 47
  • 1
  • 10
  • 1
    How does that other question not answer yours? – Jongware May 25 '15 at 09:14
  • 1
    What if there are 5 removable drives plugged in? Maybe it would be best for the user to choose. – Dialecticus May 25 '15 at 09:14
  • Hi @Dialecticus will be only 1. Because this is a machine which using windows. Or i would say i will make sure 1 removable drive only. Thanks – KevinYong May 25 '15 at 09:16
  • @Jongware Yes i couldn't understand the solution given. Especially how to use the structure dbcv_unitmask to return the drive letter? – KevinYong May 25 '15 at 09:18
  • the [first answer](http://stackoverflow.com/a/286555/33499) also answers your question. – wimh May 25 '15 at 09:20
  • I perhaps in my case i should count the last one as my removable drive? As there's no other drive plugging in after me. – KevinYong May 25 '15 at 09:26
  • 1
    I don't think it is possible to determine which removable drive was plugged in last. Such information is probably not kept available in the system. There are ways to detect changes while they happen, but I don't think that's what you want. – Dialecticus May 25 '15 at 09:55
  • "*... was plugged in last.*": @Dialecticus: One could scan the event logs? – alk May 25 '15 at 14:59
  • @alk true. Would have to translate the full USB device name to drive letter, and hope that event log is not cleared, but apart from that it looks doable. – Dialecticus May 25 '15 at 15:19

1 Answers1

2

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.

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • Okay! I can understand this. Let me try thanks ! – KevinYong May 25 '15 at 11:03
  • ya i can get the drive letter now. But couldn't differentiate if it is a storage removable media. I'm currently starting my loop from C: onwards just to avoid the possibility of floopy drives on A: or B:. – KevinYong May 26 '15 at 05:16