Name
ChDrive Procedure
Class
Microsoft.VisualBasic.FileSystem
Syntax
ChDrive(drive)-
drive Use: Required
Data Type: String or Char
The letter of the drive (A-Z) to set as the new default drive
Description
Changes the current working (default) disk drive
Rules at a Glance
If a zero-length string is supplied, the drive is not changed.
If
driveletterconsists of more than one character, only the first character is used to determine the drive.
Example
The following example demonstrates a utility function that uses
ChDrive to determine if a given drive is
available. By centralizing the test, this reduces the amount of
coding required each time you need to use
ChDrive.
Private Function IsAvailableDrive(sDrive As String) _
As Boolean
'if an error occurs goto to the next line of code
On Error Resume Next
Dim sCurDrv As String
'get the letter of the current drive
sCurDrv = Left$(CurDir, 1)
'attempt to change the drive
ChDrive(sDrive)
'did an error occur?
If Err.Number = 0 Then
'no - this drive is OK to use
IsAvailableDrive = True
Else
'yes - don't use this drive
IsAvailableDrive = False
End If
'set the drive back to what it was
ChDrive(sCurDrv)
End FunctionThe following code snippet shows how this function could be implemented within your application:
If IsAvailableDrive(sDrv) Then
ChDrive(sDrv)
Else
MsgBox ("Cannot use Drive " & sDrv & ":\")
End IfProgramming Tips and Gotchas
The current directory is unaffected by the
ChDriveprocedure.Since
ChDriveonly processes the first letter of thedrive
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access