hai Friends,
is there any way of finding the Cd drive letter like e or d or ...
srikanth
Printable View
hai Friends,
is there any way of finding the Cd drive letter like e or d or ...
srikanth
Take a look at this: http://www.vbsquare.com/tips/tip271.html
The link gets me a 404 error. I searched for the article but that link was also not working. Maybe I should inform VB Square...
Code:'get the first CD Rom drive letter and use it as default
Option Explicit
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
Const DRIVE_CDROM As Long = 5
Private Sub cmdGo_Click()
Dim strDriveLetter As String
' Call the GetFirstCdRomDriveLetter() and store the
' return value in strDriveLetter.
strDriveLetter = GetFirstCdRomDriveLetter()
' Display the drive letter in a message box.
MsgBox strDriveLetter
End Sub
Function GetFirstCdRomDriveLetter() As String
' Declare variables.
Dim lDriveType As Long
Dim strDrive As String
Dim lStart As Long: lStart = 1
' Create a string to hold the logical drives.
Dim strDrives As String
strDrives = Space(150)
' Get the logial drives on the system.
' If the function fails it returns zero.
Dim lRetVal As Long
lRetVal = GetLogicalDriveStrings(150, strDrives)
' Check to see if GetLogicalDriveStrings() worked.
If lRetVal = 0 Then
' Get GetLogicalDriveStrings() failed.
GetFirstCdRomDriveLetter = vbNullString
Exit Function
End If
' Get the string that represents the first drive.
strDrive = Mid(strDrives, lStart, 3)
Do
' Test the first drive.
lDriveType = GetDriveType(strDrive)
' Check if the drive type is a CD-ROM.
If lDriveType = DRIVE_CDROM Then
' Found the first CD-ROM drive on the system.
GetFirstCdRomDriveLetter = strDrive
Exit Function
End If
' Increment lStart to next drive in the string.
lStart = lStart + 4
' Get the string that represents the first drive.
strDrive = Mid(strDrives, lStart, 3)
Loop While (Mid(strDrives, lStart, 1) <> vbNullChar)
End Function
Another way (this will put all the CD-Rom drives in a listbox, and without using Mid, which AFAIK is a little bit faster):
Code:Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Private Const DT_CDROM As Long = 5
Private Sub EnumCDDrives(lstDest As ListBox)
Dim sDrive As String
Dim lDrives As Long
Dim lK As Long
' Get all drives
lDrives = GetLogicalDrives()
lstDest.Clear
' Check all drives
For lK = 0 To 25
If (lDrives And 2 ^ lK) <> 0 Then
' Get drive root
sDrive = Chr$(65 + lK) & ":\"
' Check Type
If GetDriveType(sDrive) = DT_CDROM Then
' Add it To the list
lstDest.AddItem sDrive
End If
End If
Next lK
End Sub
How about this?
This code returns the drives in an array, STARTING WITH 0.Code:Option Explicit
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Private Const DT_CDROM As Long = 5
Private Function CDDrive() As Variant
Dim SDRN As Byte
Dim sDrive As String
Dim Drives() As String
Dim Count As Byte
Count = 0
For SDRN = Asc("A") To Asc("Z")
sDrive = Chr$(SDRN) & ":\"
If GetDriveType(sDrive) = DT_CDROM Then
Count = Count + 1
ReDim Drives(0 To Count - 1)
Drives(Count - 1) = sDrive
End If
Next
CDDrive = Drives
End Function