|
|
![]() |
|
||
|
|
|||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Disk/Drive API Routines GetLogicalDriveStrings: An API 'DriveExists' Routine |
|
| Posted: | Wednesday October 20, 1999 |
| Updated: | Monday December 26, 2011 |
| Applies to: | VB4-32, VB5, VB6 |
| Developed with: | VB6, Windows NT4 |
| OS restrictions: | None |
| Author: | VBnet - Randy Birch |
|
Related:
|
FindFirstFile: Changing File and/or Folder Attributes Recursively
FindFirstFile: Fast Directory File Count FindFirstFile: Extract Filename from a Full Path FindFirstFile: Performance Comparison - FSO vs. API FindFirstFile: Comparison of FindFirstFile and SearchTreeForFile FindFirstFile: Save a Recursive Search of All Drives to Disk FindFirstFile: Save a Recursive Search of Specified Drives to Disk GetFileVersionInfo: File Search and File Property Info GetLogicalDriveStrings: An API 'DriveExists' Routine FindFirstFile: An API 'FileExists' Routine FindFirstFile: An API 'FolderExists' Routine PathFileExists: A Local/Network File/Folder/Drive Exists Routine |
| Prerequisites |
| None. |
|
|
For
the API purest, here's a tiny function that simply returns True if a specified drive exists, or False if not.
Using GetLogicalDriveStrings, its a simple matter of retrieving all the available drives, then performing a case-insensitive Instr() against the result to determine if the passed drive letter is present. This method will also return True if the drive passed is a mapped network drive. |
| BAS Module Code |
| None. |
|
|
| Form Code |
|
|
| Drop a combo box and a Label onto a form and add the following: |
|
|
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 Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long
Private Sub Form_Load()
Dim x As Long
'fill a combo with the letters A-Z
For x = 65 To 90
Combo1.AddItem Chr$(x) & ":"
Next
End Sub
Private Sub Combo1_Click()
'pass the selected letter to the DriveExists
'routine, returning the result to a label.
If Combo1.ListIndex > -1 Then
Label1.Caption = DriveExists(Combo1.List(Combo1.ListIndex))
End If
End Sub
Private Function DriveExists(SDrive As String) As Boolean
Dim tmp As String
Dim nBuffersize As Long
'Call the API with a buffer size of 0.
'The call fails, and the required size
'is returned as the result.
nBuffersize = GetLogicalDriveStrings(0&, tmp)
'pad a string to hold the results
tmp = Space$(nBuffersize)
nBuffersize = Len(tmp)
'and call again
If GetLogicalDriveStrings(nBuffersize, tmp) Then
'if the drive letter passed is in
'the returned logical drive string,
'return True. Use vbTextCompare for
'a case-insensitive match (remembering
'that when a compare method is specified,
'the start position must also be specified.
DriveExists = InStr(1, tmp, SDrive, vbTextCompare)
End If
End Function |
| Comments |
| This function will return the correct result regardless of format of the drive string passed .. i.e. you could pass 'M', 'M:' or 'M:\" |
|
|
|
|
|
|||||
|
|||||
|
|
|||||
|
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |