Visual Basic Disk/Drive API Routines
GetLogicalDriveStrings: Determine if a Specific Drive Exists
     
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:  

GetLogicalDriveStrings: An API 'DriveExists' Routine
FindFirstFile: An API 'FileExists' Routine
FindFirstFile: An API 'FolderExists' Routine
       
 Prerequisites
None.

Here is a simple means of determining whether a specific drive exists on the target system.

 

 BAS Module Code
Place the following API declare code into the general declarations area of a bas module. If this is a one-form project, the declares below could be placed into the general declaration section of the form instead, with all Public references changed to Private.

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.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Declare Function GetLogicalDriveStrings Lib "kernel32" _
     Alias "GetLogicalDriveStringsA" _
    (ByVal nBufferLength As Long, _
     ByVal lpBuffer As String) As Long
 Form Code
To a project form add 2 command buttons ((Command1 and Command2), and a label (Label1) as indicated in the illustration. Add the following to the form:

Option Explicit

Private Sub Form_Load()

   Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
  
End Sub


Private Sub Command2_Click()
   
   Unload Me
   
End Sub


Private Sub Command1_Click()

   Dim drv2Find As String

   drv2Find = "D"
  
   If DriveExists(drv2Find) Then
      Label1.Caption = "Drive " & drv2Find & ": exists on this system."
   Else
      Label1.Caption = "Drive " & drv2Find & ": was not found."
   End If

End Sub


Private Function DriveExists(drvName As String) As Boolean

  'retrieve the available drives, and check 
  'each against the passed drive name
  'working variables   
   Dim allDrives As String
 
  'pad the string with spaces   
   allDrives = Space$(64)
 
  'call the API to get the string containing all drives   
   Call GetLogicalDriveStrings(Len(allDrives), allDrives)
 
  'Do an InStr search, using text compare (not case sensitive).
  'If the drive letter is in the string, Instr will return 
  'greater than 0, evaluating below as True   
   DriveExists = InStr(1, allDrives, drvName, 1) > 0
    
End Function
 Comments
The nice thing about this drive check method is that it will not attempt to read the drive, making it ideal for checking for a floppy, Zip, Jazz CD-ROMor other removable drive without generating the typical "Device is not accessible" error message.

Note that a removable SCSI drive letter (like that for a Zip or SyQuest SCSI) will only be present if the device was powered on at boot and located by the SCSI manager.


 
 

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