Private Sub Form_Load()
Dim val As Integer
GetCDRom
End Sub
Private Function GetCDRom() As String
Dim LDs As Long, Cnt As Long, sDriver As String
LDs = GetLogicalDrives
For Cnt = 0 To 25
If (LDs And 2 ^ Cnt) <> 0 Then
sDriver = Chr$(65 + Cnt) & ":\"
If IsCDRom(sDriver) Then
Run sDriver
End If
End If
Next Cnt
End Function
Private Function IsCDRom(ByVal sDriver As String) As Boolean
Select Case GetDriveType(sDriver)
Case 2: IsCDRom = False 'Me.Print "Removable"
Case 3: IsCDRom = False ' Me.Print "Drive Fixed"
Case Is = 4: IsCDRom = False ' Me.Print "Remote"
Case Is = 5: IsCDRom = True ' Me.Print "Cd-Rom"
Case Is = 6: IsCDRom = False ' Me.Print "Ram disk"
Case Else: IsCDRom = False ' Me.Print "Unrecognized"
End Select
End Function
VB声明
Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
说明
获取一个字串,其中包含了当前所有逻辑驱动器的根驱动器路径
返回值
Long,装载到lpBuffer的字符数量(排除空中止字符)。如缓冲区的长度不够,不能容下路径,则返回值就变成要求的缓冲区大小。零表示失败。会设置GetLastError
参数表
参数 类型及说明
nBufferLength Long,lpBuffer字串的长度
lpBuffer String,用于装载逻辑驱动器名称的字串。每个名字都用一个NULL字符分隔,在最后一个名字后面用两个NULL表示中止(空中止)
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Sub Form_Load()
Dim strSave As String
'Set the graphic mode to persistent
Me.AutoRedraw = True
'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 and print them on the form
For keer = 1 To 100
If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
Me.Print Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
Next keer
End Sub
Private Declare Function GetVolumeInformation Lib "Kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long
Private Sub Form_Load()
Dim Serial As Long, VName As String, FSName As String
'Create buffers
VName = String$(255, Chr$(0))
FSName = String$(255, Chr$(0))
'Get the volume information
GetVolumeInformation "C:\", VName, 255, Serial, 0, 0, FSName, 255
'Strip the extra chr$(0)'s
VName = Left$(VName, InStr(1, VName, Chr$(0)) - 1)
FSName = Left$(FSName, InStr(1, FSName, Chr$(0)) - 1)
MsgBox "The Volume name of C:\ is '" + VName + "', the File system name of C:\ is '" + FSName + "' and the serial number of C:\ is '" + Trim(Str$(Serial)) + "'", vbInformation + vbOKOnly, App.Title
End Sub
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Sub Command1_Click()
'函数返回值:
'0: 未知驱动器
'1: 软驱等可移动驱动器
'2: 固定驱动器
'3: 网络驱动器
'4: 光驱
'5: RAM驱动器
Dim DrvNum As Integer: Dim DrvVal As Long: Dim DrvCode As String
For DrvNum = 0 To 25
DrvVal = GetDriveType(Chr(DrvNum + 65) + ":") '依次对A-Z进行检测
If DrvVal = 5 Then
DrvCode = Chr$(DrvNum + 65)
CD_Check = DrvCode
End If
Next
MsgBox "光驱" & DrvCode
End Sub