Visual Basic Disk/Drive API Routines
GetDriveType: Enumerate and Identify Available System Drives
     
Posted:   Sunday January 26, 1997
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB4-32, Windows 95
OS restrictions:   None
Author:   VBnet - Randy Birch
     
Related:   DeviceIoControl: Determine Media Type for CD/DVD Drives
IsNetDrive: Determining the Connection State of a Mapped Drive
DeviceIoControl: Obtain Physical Drive Information
     
 Prerequisites
None.

One of my oldest code samples updated for VB6 under XP, the code shown here provides two methods resulting in a list box being populated with the current drives on the system, as well as each drive's type.

The first set or routines (initiated through Command1) uses a custom method (StripNulls) that removes and returns each of the drives in turn from the system drive list.  The Command1 set of routines can be used on any 32-bit VB version.

The second set (Command2) targets VB6 specifically by using the Replace and Split functions to achieve the same results as StripNulls. 

The end result of both set of routines is exactly the same, as shown.

For the remote drives returned, an additional call could be made to WNetGetConnection to return the path related to the remote drives -- see WNetGetConnection: Get UNC Path for Mapped Drive.

 BAS Module Code
None.

 Form Code
To a project form add 2 command buttons (Command1 and Command2) and a list box (List1). Labels are optional. Add the following to the form:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const DRIVE_REMOVABLE As Long = 2
Private Const DRIVE_FIXED As Long = 3
Private Const DRIVE_REMOTE As Long = 4
Private Const DRIVE_CDROM As Long = 5  'can be a CD or a DVD
Private Const DRIVE_RAMDISK As Long = 6
      
Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
     Alias "GetLogicalDriveStringsA" _
    (ByVal nBufferLength As Long, _
     ByVal lpBuffer As String) As Long

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



Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
   Command1.Caption = "Get Available Drives (all VB versions)"
   Command2.Caption = "Get Available Drives (VB6 only)"
   
End Sub


Private Sub Form_DblClick()

   List1.Clear
   
End Sub


Private Sub Command1_Click()

  'VB4-32 / VB5 / VB6-compatible code
  
   Dim sAllDrives As String
   Dim sDrive As String
   Dim sDrvType As String
 
  'get the list of all drives
   sAllDrives = GetDriveString()
 
  'separate the drive strings and
  'retrieve the drive type
   Do Until sAllDrives = Chr$(0)
   
      sDrive = StripNulls(sAllDrives)
      sDrvType = GetDriveDescription(sDrive)

      List1.AddItem sDrive & vbTab & sDrvType
    
   Loop

End Sub


Private Sub Command2_Click()

  'VB6-only

   Dim sAllDrives As String
   Dim sDrvType As String
   Dim sDrives() As String
   Dim cnt As Long
   
  'get the list of all drives
   sAllDrives = GetDriveString()
   
  'Change nulls to spaces, then trim.
  'This is required as using Split()
  'with Chr$(0) alone adds two additional
  'entries to the array drives at the end
  'representing the terminating characters.
   sAllDrives = Replace$(sAllDrives, Chr$(0), Chr$(32))
   sDrives() = Split(Trim$(sAllDrives), Chr$(32))

   For cnt = LBound(sDrives) To UBound(sDrives)
         
      sDrvType = GetDriveDescription(sDrives(cnt))

      List1.AddItem sDrives(cnt) & vbTab & sDrvType
    
   Next
   
End Sub


Private Function GetDriveString() As String

  'Used by both demos
  
  'returns string of available
  'drives each separated by a null
   Dim sBuff As String
   
  'possible 26 drives, three characters
  'each plus a trailing null for each
  'drive letter and a terminating null
  'for the string
   sBuff = Space$((26 * 4) + 1)
  
  If GetLogicalDriveStrings(Len(sBuff), sBuff) Then

     'just trim off the trailing spaces - leave the nulls
      GetDriveString = Trim$(sBuff)
      
   End If

End Function


Private Function GetDriveDescription(RootPathName) As String
 
  'Used by both demos
  
  'Passed is the drive to check.
  'Returned is the type of drive.
   Select Case GetDriveType(RootPathName)
      Case 0: GetDriveDescription = "The drive type cannot be determined"
      Case 1: GetDriveDescription = "The root directory does not exist"

      Case DRIVE_REMOVABLE:
          Select Case Left$(RootPathName, 1)
              Case "a", "b": GetDriveDescription = "Floppy drive"
              Case Else: GetDriveDescription = "Removable drive"
          End Select

      Case DRIVE_FIXED:   GetDriveDescription = "Hard drive; can not be removed"
      Case DRIVE_REMOTE:  GetDriveDescription = "Remote (network) drive"
      Case DRIVE_CDROM:   GetDriveDescription = "Optical drive (CD or DVD)"
      Case DRIVE_RAMDISK: GetDriveDescription = "RAM disk"
   End Select
  
End Function


Private Function StripNulls(startstr As String) As String

  'Routine used by older VB4-32 / VB5 / VB6 code demo only
 
  'Take a string separated by chr$(0)
  'and split off 1 item, shortening the
  'string so next item is ready for removal.
   Dim pos As Long

   pos = InStr(startstr$, Chr$(0))
  
   If pos Then
   
      StripNulls = Mid$(startstr, 1, pos - 1)
      startstr = Mid$(startstr, pos + 1, Len(startstr))
   
   End If

End Function
 Comments
If you're not using VB6, remember to comment out or delete the code in Command2 to prevent compile errors.

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter