Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Need to check if CD drive is closed and if it has a disc inserted


Reply
 
Thread Tools Display Modes
  #1  
Old 10-05-2004, 06:27 AM
Arphahat Arphahat is offline
Newcomer
 
Join Date: Apr 2004
Posts: 18
Default 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?
Reply With Quote
  #2  
Old 10-05-2004, 07:57 AM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
 
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
Default

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
__________________
Regards
John, jlsd.co.uk

Last edited by Jonny; 10-05-2004 at 08:02 AM.
Reply With Quote
  #3  
Old 10-05-2004, 11:04 AM
Arphahat Arphahat is offline
Newcomer
 
Join Date: Apr 2004
Posts: 18
Default

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.
Reply With Quote
  #4  
Old 10-05-2004, 06:55 PM
Jonny's Avatar
Jonny Jonny is offline
Senior Contributor
 
Join Date: Jun 2003
Location: Birmingham, England, UK
Posts: 821
Default

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
__________________
Regards
John, jlsd.co.uk
Reply With Quote
  #5  
Old 10-05-2004, 10:18 PM
evil13's Avatar
evil13 evil13 is offline
Centurion
 
Join Date: Feb 2004
Location: VietNam
Posts: 130
Default

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?
Reply With Quote
  #6  
Old 10-05-2004, 11:15 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

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
Reply With Quote
  #7  
Old 10-10-2004, 03:37 AM
Cerryl's Avatar
Cerryl Cerryl is offline
Junior Contributor
 
Join Date: Jul 2004
Location: UK
Posts: 231
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->