• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Display drive letters

I've got a dialog based app. using VC++ 5.0.  I want to display a list of the available drives (no files or subdirectories).  I don't need anything too fancy.  Right now, I'm just trying to use a List Box populated with DlgDirList(), but I don't know what to use for the parent path for all of the drives.  I'm open to using something other than a list box.  
0
halen
Asked:
halen
  • 4
1 Solution
 
cpopinCommented:
Try something like this:

CString sDrive;
for (char drive = 'A'; drive <= 'Z'; ++drive) {
      sDrive.Format("%c:\\");
      if (GetDriveType(sDrive) != DRIVE_UNKNOWN) {
            sDrive.Format("%c");
            m_lbDrives.AddString(sDrive);
      }
}
0
 
cpopinCommented:
...and if you'd like to see the same code w/o EE removing the indention:

CString sDrive;
for (char drive = 'A'; drive <= 'Z'; ++drive) {
    sDrive.Format("%c:\\");
    if (GetDriveType(sDrive) != DRIVE_UNKNOWN) {
        sDrive.Format("%c");
        m_lbDrives.AddString(sDrive);
    }
}
0
 
cpopinCommented:
Oops, that doesn't work...try this:

    CString sDrive;
    DWORD dwDrives = GetLogicalDrives();
    for (char drive = 'A'; drive <= 'Z'; ++drive, dwDrives >>= 1) {
        if (dwDrives & 1) {
            sDrive.Format("%c", drive);
            m_lbDrives.AddString(sDrive);
        }
    }
0
 
halenAuthor Commented:
It wasn't quite as straight forward as I had hoped, but it wasn't an excessive amount of code and it accomplished what I wanted.
0
 
cpopinCommented:
GetLogicalDrives() returns a bit value with one bit turned on for each of the 26 drive letters.  It's kind of lame, but efficient.

GetLogicalDriveStrings() is more straight forward because it gives you a list of null-terminated strings represending drive paths.  The list ends with a double null chararter, like this:

a:\<null>c:\<null>d:\<null><null>
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now