I want to enumerate all available drive letters (which aren't already taken) in Windows using VC++.
How can I do this?
Click on the Start menu, type CMD in the search box, then right-click on it and select Run as administrator. Or press WIN + R, type CMD, and press Enter. Type diskpart in CMD to start it. Step 2: Type list disk and press Enter.
You're not just limited to 26 drives on modern versions of Windows. You can also change which drives use which letters from Disk Management—although, you can't change your C: drive to another letter. Even changing a letter like D: to E: can cause problems.
Right-click on "Command Prompt" and choose "Run as Administrator". At the prompt, type "diskpart" and hit Enter. At the diskpart prompt type "list disk". This will list all the hard drives in the system.
::GetLogicalDrives() returns a list of available (read: used) drives as bits in a mask. This should include mapped network drives. Thus, you can simply walk the bits to find bits that are zero, meaning no drive is present. If in doubt, you can always call ::GetDriveType() with the drive letter + ":\"
(":\\"
in C code, or _T(":\\")
in Unicode-aware terminology, of course), and that should return DRIVE_UNKNOWN
or DRIVE_NO_ROOT_DIR
if the drive is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With