函 数:
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Public Function CheckDrive() As Boolean
Dim StrDrive As String
Dim DriveID As String
Dim i As Integer
Dim m As Long
Dim myDrive As Object
CheckDrive = False
StrDrive = String(100, Chr$(0)) '初始化盘符串
m = GetLogicalDriveStrings(100, StrDrive) '返回盘符串
For i = 1 To 100 Step 4 '注意这里是4
DriveID = Mid(StrDrive, i, 3) '枚举盘符
If DriveID = Chr$(0) & Chr(0) & Chr(0) Then Exit For '没有盘符,即时退出循环
If GetDriveType(DriveID) = 2 Then
Set myDrive = CreateObject("Scripting.FileSystemObject").GetDrive(DriveID)
If Not myDrive.IsReady Then Exit Function '如果磁盘不可用,就终止函数
If myDrive.VolumeName = "RECOVERY" And myDrive.SerialNumber = "-1634556752" Then '“-1634556752”是我的U盘系列码,“RECOVERY”是我的U盘卷标,
CheckDrive = True
End If
End If
Next i
End Function
用 法:
If CheckDrive Then
MsgBox "验证成功!"
Else
MsgBox "验证失败!"
End If
预设U盘的SerialNumber和VolumeName,插入U盘后调用函数CheckDrive,返回Ture或者False。
参考文章:
通过FileSystemObject获取驱动器信息[Access软件网]
http://www.accessoft.com/article-show.asp?id=11459