Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Finding Available drive letters


I need to obtain a listing of drive letters that are currently not being used by the local system running the application.  These drive letters will be used to populate a combo box, which the user can select.  How is this action performed?

0
escheider
Asked:
escheider
  • 4
  • 2
1 Solution
 
g0rathCommented:
Option Explicit
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Sub PopulateComboBox()
      Dim driveType As Integer
      Dim NextDrive As String
      Dim i As Integer
     
      For i = 0 To 25
        NextDrive = Chr(i + 65) + ":\"
        driveType = GetDriveType(NextDrive)
        If driveType = 1 Then
            Combo1.AddItem NextDrive
        End If
        Debug.Print NextDrive
      Next i
End Sub
   
Private Sub Form_Load()
    PopulateComboBox
End Sub
      
0
 
g0rathCommented:
btw from the API docs, here are the constants

DRIVE_CDROM = 5
A CD-ROM drive.
DRIVE_FIXED = 3
A hard drive.
DRIVE_RAMDISK = 6
A RAM disk.
DRIVE_REMOTE = 4
A network drive or a drive located on a network server.
DRIVE_REMOVABLE = 2
0
 
g0rathCommented:
yeah and of course

DRIVE_UNKNOWN = 1
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ryan ChongCommented:
Or try this:

in a module:

Public Declare Function GetLogicalDrives Lib "kernel32" () As Long
Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Sub GetDrives(ByRef myControl As Object, ByVal Select1stItem As Boolean, Optional ClearItems As Boolean = True, Optional ReturnCompleteDriveName As Boolean = True, Optional ReturnCDROMDriveOnly As Boolean = False)
    On Error GoTo ErrHandler
    Dim LDs As Long, cnt As Long, strSave As String
       
    If ClearItems = True Then myControl.Clear
   
    If ReturnCompleteDriveName = True Then
        'Create a buffer to store all the drives
        strSave = String(255, Chr$(0))
        'Get all the drives
        LDs = GetLogicalDriveStrings(255, strSave)
        'Extract the drives from the buffer and print them on the form
        For cnt = 1 To 25
            If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
            If ReturnCDROMDriveOnly = True Then
                If GetVolumnInfo(Left$(strSave, InStr(1, strSave, Chr$(0)) - 1), viFileSystem) = "CDFS" Then
                    myControl.AddItem Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
                End If
            Else
                myControl.AddItem Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
            End If
            strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
        Next cnt
    Else
        'get the available drives
        LDs = GetLogicalDrives
        For cnt = 0 To 25
            If (LDs And 2 ^ cnt) <> 0 Then
                If ReturnCDROMDriveOnly = True Then
                    If GetVolumnInfo(Chr$(65 + cnt), viFileSystem) = "CDFS" Then
                        myControl.AddItem Chr$(65 + cnt)
                    End If
                Else
                    myControl.AddItem Chr$(65 + cnt)
                End If
            End If
        Next cnt
    End If
   
    If Select1stItem = True And myControl.ListCount > 0 Then myControl.ListIndex = 0
    Exit Sub
ErrHandler:
    ShowErrMsg
End Sub

then use it like:

GetDrives Combo1, True, True

Hope this helps too
0
 
g0rathCommented:
well really you should look up 0 or 1 since it's different in the old days....so if you're concerned with OS compatiblilty

DRIVE_UNKNOWN = 0
DRIVE_NO_ROOT_DIR = 1
0
 
Ryan ChongCommented:
and paste this in the module as well:

Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Public Enum VolumnInfo
    viVolumn = 1
    viFileSystem = 2
    viSerial = 3
End Enum
Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

Public Function GetVolumnInfo(ByVal DriveName As String, ByVal ReturnInfo As VolumnInfo)
    Dim Serial As Long, VName As String, FSName As String
   
    If Len(DriveName) > 3 Then DriveName = Left$(DriveName, 3)
   
    If InStr(1, DriveName, ":") = 0 Then DriveName = DriveName & ":"
   
    'Create buffers
    VName = String$(255, Chr$(0))
    FSName = String$(255, Chr$(0))
    'Get the volume information
    GetVolumeInformation DriveName, VName, 255, Serial, 0, 0, FSName, 255
    'Strip the extra chr$(0)'s
    VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
    FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
    Serial = Trim(Str$(Serial))
    Select Case ReturnInfo
    Case 1
        GetVolumnInfo = VName
    Case 2
        GetVolumnInfo = FSName
    Case 3
        GetVolumnInfo = Serial
    End Select
End Function
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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