hi,
how can i determine the drive letter of a PC's CD-Rom?
Thanks in advance
Printable View
hi,
how can i determine the drive letter of a PC's CD-Rom?
Thanks in advance
try this sample from Kaverin,
VB Code:
'Author Kaverin 'in a module 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 Function GetCDDriveLetter() As String Dim strBuffer As String Dim astrDrives As Variant Dim lngLength As Long Dim i As Integer Const CDROM As Long = 5 'make buffer strBuffer = Space$(256) 'get all drive strings lngLength = GetLogicalDriveStrings(Len(strBuffer), strBuffer) 'chop off the unnecessary part strBuffer = Left$(strBuffer, lngLength) 'break up the string into the drives astrDrives = Split(strBuffer, vbNullChar) For i = 0 To UBound(astrDrives) If GetDriveType(astrDrives(i)) = CDROM Then 'if the drive is a cd rom, we're done, so return the letter GetCDDriveLetter = Left$(astrDrives(i), 1) Exit Function End If Next i 'if you get to this point, there is no cd rom, so return "" GetCDDriveLetter = "" End Function
Code:Private Declare Function GetDriveType Lib "kernel32" Alias _
"GetDriveTypeA" (ByVal nDrive As String) As Long
Private Sub Form_Load()
Dim intLoopCounter As Integer
For intLoopCounter = 65 To 90
If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
MsgBox "The first CD-ROM drive is :" & Chr(intLoopCounter)
Exit For
End If
Next intLoopCounter
End Sub
It is becoming more frequent that PCs have more than one CD Rom drive. If anyone has a better way of determing all CD Rom drives, that would be very cool. This works, but it returns all mapped drives, rather than just the CD Rom drives.VB Code:
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 Private Function GetDriveTypeByLetter(MyDriveLetter As String) As String Dim MyDrive As String If Right(MyDriveLetter, 1) <> "\" Then MyDrive = MyDriveLetter & "\" Else MyDrive = MyDriveLetter End If Select Case GetDriveType(MyDrive) Case 2 GetDriveTypeByLetter = "Removable" Case 3 GetDriveTypeByLetter = "Fixed Hard Drive" Case 4 GetDriveTypeByLetter = "Remote" Case 5 GetDriveTypeByLetter = "CD-ROM" Case 6 GetDriveTypeByLetter = "Ram disk" Case Else GetDriveTypeByLetter = "Unrecognized" End Select End Function Private Sub Form_Load() Dim SaveIt As String Dim i As Long Dim MyDrives As Long SaveIt = String(255, Chr$(0)) MyDrives = GetLogicalDriveStrings(255, SaveIt) For i = 1 To 100 If Left$(SaveIt, InStr(1, SaveIt, Chr$(0))) = Chr$(0) Then Exit For List1.AddItem Left$(SaveIt, InStr(1, SaveIt, Chr$(0)) - 1) & vbTab & GetDriveTypeByLetter(Left$(SaveIt, InStr(1, SaveIt, Chr$(0)) - 1)) SaveIt = Right$(SaveIt, Len(SaveIt) - InStr(1, SaveIt, Chr$(0))) Next End Sub
There is a simpler way - my code loops through A-Z, just picks the first up & then exits the function, however, changing a few lines - will make it return all of the CD rom drives no problem ;) :
Code:Private Declare Function GetDriveType Lib "kernel32" Alias _
"GetDriveTypeA" (ByVal nDrive As String) As Long
Private Sub Form_Load()
Dim intLoopCounter As Integer, intAryCounter As Integer
Dim strAryCDDrives() As String
intAryCounter = 0
For intLoopCounter = 65 To 90
If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
ReDim strAryCDDrives(intAryCounter + 1)
strAryCDDrives(intAryCounter) = Chr(intLoopCounter)
intAryCounter = intAryCounter + 1
End If
Next intLoopCounter
End Sub
Sorry, getting complicated - that one was putting them in an array, this is a simple one that does the same to a string. Hope it helpsCode:Private Declare Function GetDriveType Lib "kernel32" Alias _
"GetDriveTypeA" (ByVal nDrive As String) As Long
Private Sub Form_Load()
Dim intLoopCounter As Integer
Dim strCDDrives As String
For intLoopCounter = 65 To 90
If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
strCDDrives = strCDDrives & chr(intLoopCounter) & VBCrlf
End If
Next intLoopCounter
Msgbox "CD Rom Drives On this PC are : " & _
VBCrlf & VBCrlf & strCDDrives
End Sub
Very cool Alex! :D