 |
 |

10-05-2004, 06:27 AM
|
|
Newcomer
|
|
Join Date: Apr 2004
Posts: 18
|
|
Need to check if CD drive is closed and if it has a disc inserted
|
I need to check to see if a CD drive is open or closed and if it has a disc in the drive. I have searched through the forum and found some code regarding checking if a disc is in the drive, but it doesn't make a distinction between an open and closed drive.
My program needs to check for data on a disc. If it doesn't find the data, it ejects the drive and asks the user to insert the proper disc. When the user inserts the disc and clicks "OK" it should proceed properly. However, I am encountering a problem where it is checking the CD again after the "OK" is clicked, but it hasn't yet registered that there is data on the new disc, so it returns to the "insert proper disc" screen unnecessarily.
Any ideas?
|
|

10-05-2004, 07:57 AM
|
 |
Senior Contributor
|
|
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
|
|
the following will firstly check if the specified drive is ready (ie, closed with a cd mounted) then if the drive is ready it will check to see if any files exist on the drive.
Code:
Private Sub Check4Files()
On Error Resume Next
Dim result As String
result = Dir("D:\", vbNormal)
If Err.Number = 52 Then
MsgBox "Drive not ready."
Timer1.Enabled = True 'Start checking for mounted cd again
Else
If result = "" Then
MsgBox "No files found."
Else
'do what you want to do
'when there are files found
End if
End If
End Sub
to check when the drive is closed you can use something like this...
Code:
Private Sub Timer1_Timer()
On Error Resume Next
Dim result As String
result = Dir("D:\", vbNormal)
If Err.Number = 52 Then
MsgBox "Drive not ready."
Else
Timer1.Enabled = False 'stop timer so we dont do this again
Check4Files 'when we have a cd call the Check4Files sub
End If
End Sub
|
Last edited by Jonny; 10-05-2004 at 08:02 AM.
|

10-05-2004, 11:04 AM
|
|
Newcomer
|
|
Join Date: Apr 2004
Posts: 18
|
|
Thanks!
But, I notice you used the same check for the door being open as well as a disc in the drive, namely the "Dir" command. If the door is closed, but there is no disc, I will get the same error as if the door is open.
|
|

10-05-2004, 06:55 PM
|
 |
Senior Contributor
|
|
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
|
|
|
well... you will have to go to a lower level of programming than vb if you need this information. I wrote some ASM for college that detects this... but you would have to find a way of using it in your vb app as i dont have time to make it portable.
Give me a post if you want to use it and i will dig it out
|
|

10-05-2004, 10:18 PM
|
 |
Centurion
|
|
Join Date: Feb 2004
Location: VietNam
Posts: 130
|
|
Small problem in your code. You check driver D
Quote:
result = Dir("D:\", vbNormal)
If Err.Number = 52 Then
|
so only machine have CD drive name D run well with your code.
Is there anyway to check out how many driver and which is CD driver?
|
|

10-05-2004, 11:15 PM
|
 |
Hydrogen Powered
Administrator * Expert *
|
|
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
|
|
Quote:
|
Originally Posted by evil13
Small problem in your code. You check driver D so only machine have CD drive name D run well with your code.
Is there anyway to check out how many driver and which is CD driver?
|
You can use the API function GetLogicalDrives to determine what drive letters exist on a system, and then based on that information you can scan them to determine what type they are with GetDriveType.
|
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
|

10-10-2004, 03:37 AM
|
 |
Junior Contributor
|
|
Join Date: Jul 2004
Location: UK
Posts: 231
|
|
Use webbone's suggestions to enumerate all CD drives then try this to determine whether a CD drive is open or closed and if it has a disc in the drive.
Code:
Public Function IsCDDriveOK(ByVal DriveToCheck As String) As Boolean
Dim Buffer As String
' clear previous errors
Err.Clear
On Error Resume Next
' CurDir$ checks to see if drive exists
Buffer = CurDir$(DriveToCheck)
If Err.Number > 0 Then
MsgBox "Drive does not exist"
Else
' Dir$ check to see if CD present
Buffer = Dir$(Left$(DriveToCheck, 1) & ":\")
If Err.Number > 0 Then
MsgBox "CD not present"
End If
End If
' return result based on errors
IsCDDriveOK = (Err.Number = 0)
End Function
|
__________________
I've got so many things worrying me...the polar cap is melting, the continental shelves are shifting, the rain forest is dying, the sea is being poisoned...and I ain't had a bit for months!
Rodney Trotter
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|