How to Find Next Available Drive Letter (for Network Connect)Last reviewed: March 8, 1996Article ID: Q141020 |
The information in this article applies to:
SUMMARYThe Visual Basic program in this article shows by example how to find the next available (unused) drive letter in Windows. This is useful when making network connections to a new drive letter.
MORE INFORMATION
Step-by-Step ExampleThe Freedrive function defined below returns the next drive letter available in Windows, followed by a colon (:).
Private Declare Function GetDriveType Lib "kernel32" Alias"GetDriveTypeA" (ByVal nDrive As String) As Long #Else Private Declare Function GetDriveType Lib "kernel" (ByVal nDrive AsInteger) As Integer #End If Function Freedrive() Dim DriveNum As Integer, FirstFreeDrive As String Dim FirstDrive As Integer DriveNum = -1 Do DriveNum = DriveNum + 1 ' start at drive zero. #If Win32 Then NextDrive$ = Chr$(DriveNum + 65) + ":\" FirstDrive% = GetDriveType(NextDrive$) #Else FirstDrive% = GetDriveType(DriveNum) #End If ' GetDriveType returns zero if it cannot determine drive ' type or returns 1 if the specified drive does not exist. #If Win32 Then Loop Until FirstDrive% = 1 #Else Loop Until FirstDrive% = 0 #End If ' DriveNum of 0 means Drive A, 1=B, 2=C, 3=D, 4=E, 5=F, and so on: FirstFreeDrive = Chr$(DriveNum + 65) + ":" Freedrive = FirstFreeDriveEnd Function
|
Additional reference words: 2.00 3.00 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |