Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Determine Drive Types and Next Available Drive Letter

When dealing with drive types and letters it is nice to have a simple function to give you that information. The function shown below will cycle through each of the valid drives on the system running the program and tell you what each type is. Once it is finished, it tells you what the next available drive letter is. In dealing with network applications, it is sometimes necessary to add a drive letter for a new connection.

The first thing you will need is an API declaration in the form using the function or in a global module. This declaration looks like the following:

Declare Function GetDriveType _
    Lib "kernel32" Alias "GetDriveTypeA" ( _
        ByVal nDrive As String) _
    As Long

The next thing you will need is the function to actually perform the work for you. The function looks like the following:

Function FreeDrive() As String
    Dim DriveNum As String    'To cycle through drive letters in order
    Dim DriveType As Long     'To hold the type of drive it is
    DriveNum = 64              'Prime the variable to be used in the loop
    Do
        DriveNum = DriveNum + 1   ' start at drive zero.
        DriveType = GetDriveType(Chr$(DriveNum) & ":\")
        ' If we are past C: and the drive type is indeterminate, exit the Loop
        If DriveType = 1 And DriveNum > 67 Then Exit Do
        Select Case DriveType
            Case 0: MsgBox Chr$(DriveNum) + ": is An Unknown type"
            Case 1: MsgBox Chr$(DriveNum) + ": Does Not Exist"
            Case 2: MsgBox Chr$(DriveNum) + ": is a Removable Drive"
            Case 3: MsgBox Chr$(DriveNum) + ": is a Fixed Drive"
            Case 4: MsgBox Chr$(DriveNum) + ": is a Remote Drive"
            Case 5: MsgBox Chr$(DriveNum) + ": is a CD-ROM Drive"
            Case 6: MsgBox Chr$(DriveNum) + ": is a RAM Drive"
        End Select
    Loop
    FreeDrive = Chr$(DriveNum) + ":"  'Return the next available drive letter
End Function

Finally, you need to place the code in an event procedure or other routine to call the FreeDrive Function. In this case, we are simply calling the function by placing the return value into a message box.

Private Sub Form_Click()
    MsgBox "Next Available Drive letter is " & FreeDrive()
End Sub
--------------------------------------------
Original tip and project submitted 
by Jeff Hargett from VB4UandMe. 
Minor text editing by Burt Abreu.
--------------------------------------------

Download VB4 Sample





Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.