1,105,395 Community Members

Check if CD ROM is present or not

Member Avatar
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 29 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 9 [?]
 
0
 

My PC has 3 drives C, D, E...when I insert the removable CD drive it should detect that the drive is the F:...
it may vary for different systems...how can I find which will can be the CD drive...

I had written the following code

Try
    For Each drive In DriveInfo.GetDrives()
        If drive.DriveType = DriveType.CDRom Then
           MessageBox.Show(drive.ToString())
           CD_Targetpath = drive.ToString
        Else
           MessageBox.Show("CD drive does not exist")
           Exit Sub
        End If
    Next
Catch ex As Exception
      MsgBox(ex.Message)
End Try

But as soon as it detects any other drive it exits from the sub

Member Avatar
nmaillet
Posting Pro
535 posts since Aug 2008
Reputation Points: 97 [?]
Q&As Helped to Solve: 106 [?]
Skill Endorsements: 7 [?]
 
0
 

You're calling Exit Sub in each iteration when the current item isn't a CD-Rom. You can use a boolean variable instead:

Dim failed As Boolean
failed = True
For Each drive In DriveInfo.GetDrives()
    If drive.DriveType = DriveType.CDRom Then
       MessageBox.Show(drive.ToString())
       CD_Targetpath = drive.ToString
       failed = False
    End If
Next
If failed Then
   MessageBox.Show("CD drive does not exist")
   Exit Sub
End If

Now you'll still have to work on what happens when multiple CD-Rom drives exist.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: