Solved

Finding Available drive letters

Posted on 2004-03-30
6
296 Views
Last Modified: 2010-05-02

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
Comment
Question by:escheider
  • 4
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
g0rath earned 50 total points
ID: 10716981
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
 
LVL 5

Expert Comment

by:g0rath
ID: 10716997
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
 
LVL 5

Expert Comment

by:g0rath
ID: 10717020
yeah and of course

DRIVE_UNKNOWN = 1
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10717039
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
 
LVL 5

Expert Comment

by:g0rath
ID: 10717088
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
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 10717126
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now