# Re: 如何寻找光碟机所在路径?

Posted by 彭明柳 on May 09, 1998 at 21:46:30:

In Reply to: 如何寻找光碟机所在路径? posted by snow on May 09, 1998 at 09:52:17:

: 我想要写一个能读取光碟图档的程式..
: 因为光碟巢不确定..可能是D: OR E:等..
: 要如何侦测...程式该如何写....??

: 谢谢帮忙... SNOW
使用API:

Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long

Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Public Const DRIVE_CDROM = 5

'在表单上插入一个命令按钮(cmdCDInfo)
'在表单上插入一个Label (lblCDROM)
private cmdCDInfo_click()
'取得所有drives的资讯
Dim r As Long
Dim DriveType As Long
Dim allDrives As String
Dim JustOneDrive As String
Dim CDLabel As String
Dim pos As Integer
Dim CDfound As Boolean
allDrives$ = Space$(64)
'以api取得所有的磁碟
r& = GetLogicalDriveStrings(Len(allDrives$), allDrives$)
'字串处理
allDrives$ = Left$(allDrives$, r&)
Do
pos% = InStr(allDrives$, Chr$(0))
If pos% Then
JustOneDrive$ = Left$(allDrives$, pos% - 1)
allDrives$ = Mid$(allDrives$, pos% + 1, Len(allDrives$))
'以api取得让drive的type
DriveType& = GetDriveType(JustOneDrive$)
If DriveType& = DRIVE_CDROM Then
CDfound = True
CDLabel$ = rgbGetVolumeLabel(JustOneDrive$)
Exit Do
End If
End If
Loop Until allDrives$ = "" Or DriveType& = DRIVE_CDROM

If CDfound Then
lblCDROM = "The CD ROM drive on your system is drive " & UCase$(JustOneDrive$) & vbCrLf
lblCDROM = lblCDROM & "The volume label is " & CDLabel$
Else: lblCDROM = "No CD ROM drives were detected on your system."
End If
End Sub

Private Function rgbGetVolumeLabel(CDPath$) As String
Dim r As Long
Dim DrvVolumeName As String
Dim pos As Integer
Dim UnusedVal1 As Long
Dim UnusedVal2 As Long
Dim UnusedVal3 As Long
Dim UnusedStr As String
DrvVolumeName$ = Space$(14)
UnusedStr$ = Space$(32)
r& = GetVolumeInformation(CDPath$, _
DrvVolumeName$, _
Len(DrvVolumeName$), _
UnusedVal1&, UnusedVal2&, _
UnusedVal3&, _
UnusedStr$, Len(UnusedStr$))
If r& = 0 Then Exit Function
pos% = InStr(DrvVolumeName$, Chr$(0))
If pos% Then DrvVolumeName$ = Left$(DrvVolumeName$, pos% - 1)
If Len(Trim$(DrvVolumeName$)) = 0 Then DrvVolumeName$ = "(no label)"
rgbGetVolumeLabel = DrvVolumeName$
End Function

Follow Ups: