'''尝试运行此代码
Const CheckDrive_NotRem = 0
Const CheckDrive_RemAndFound = 1
Const CheckDrive_RemAndNotFound = 2
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
'InPut: the letter of the drive to check it or the string "all" to check for all drives
Public Function CheckNow(strDrive As String)
Dim strSave As String
Dim strDriveName As String
Dim Res, keer, DiskFound
'Create a buffer to store all the drives
strSave = String(255, Chr$(0))
'Get all the drives
ret& = GetLogicalDriveStrings(255, strSave)
'Extract the drives from the buffer
For keer = 1 To 100
If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
strDriveName = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
'Check the drive for type and diskettes
If strDrive = "all" Then
Res = CheckDrive(strDriveName)
If Res = CheckDrive_RemAndNotFound Then
MsgBox "No Disk in drive: " & strDriveName
ElseIf Res = CheckDrive_RemAndFound Then
MsgBox "There is a Disk in drive: " & strDriveName
End If
ElseIf (strDrive + ":\") = strDriveName Then
Res = CheckDrive(strDriveName)
If Res = CheckDrive_RemAndNotFound Then
MsgBox "No Disk in drive: " & strDriveName
ElseIf Res = CheckDrive_RemAndFound Then
MsgBox "There is a Disk in drive: " & strDriveName
ElseIf Res = CheckDrive_NotRem Then
MsgBox "This isn't a removal or CD-Rom drive"
End If
DiskFound = True
Exit For
End If
strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
Next keer
'Check whether there is a drive with the letter the user entered
If (Not DiskFound) And (strDrive <> "all") Then MsgBox "No drive with this letter found!"
End Function
''''''''''''''''''''''''
'Task:
'1) Check whether the drive is removal or CD-Rom or not
'2) Check if there is a disk in the drive
'The idea depends on changing the directory to the drive
'path then check: if an error happens that means the program
'can't change the directory to the path of the disk.
'Consequently, there is no disk, and the function returns
'
'OutPut of CheckDrive:
' 0 = The drive isn't removal or CD-Rom
' 1 = There is a Disk in the drive
' 2 = There is no Disk in the drive
''''''''''''''''''''''''
Function CheckDrive(strDrive As String) As Integer
On Error GoTo errhandler
Select Case GetDriveType(strDrive)
Case 2 ' Removal
ChDir strDrive
CheckDrive = CheckDrive_RemAndFound ' The drive is removal and there is a disk
Exit Function
Case 5 ' CD-Rom drive
ChDir strDrive
CheckDrive = CheckDrive_RemAndFound ' The drive is CD-Rom and there is a disk
Exit Function
End Select
Exit Function
errhandler:
'there isn't a Disk in the drive
If Err.Number = 75 Then CheckDrive = 2 'Path/access error
End Function