Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerating all available drive letters in Windows

Tags:

c++

winapi

drives

I want to enumerate all available drive letters (which aren't already taken) in Windows using VC++.

How can I do this?

like image 241
sharkin Avatar asked Nov 13 '08 08:11

sharkin


People also ask

How can I get a list of available drives?

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.

How many drive letters are available?

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.

How do I list all hard drives in Windows?

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.


1 Answers

::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.

like image 159
JTeagle Avatar answered Sep 22 '22 05:09

JTeagle