Solved

Finding Available drive letters

Posted on 2004-03-30
6
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 52

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 52

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

688 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