4

I am using the following code to get a list of the letters for each drive on my computer. I want to get the drive letter of the CD drive from this list. How do I check it?

The code I am using to get list is as below:

In the Form.Load event:

    cmbDrives.DropDownStyle = ComboBoxStyle.DropDownList
    Dim sDrive As String, sDrives() As String

    sDrives = ListAllDrives()

    For Each sDrive In sDrives

    Next
    cmbDrives.Items.AddRange(ListAllDrives())

. . .

Public Function ListAllDrives() As String()
    Dim arDrives() As String
    arDrives = IO.Directory.GetLogicalDrives()
    Return arDrives
End Function
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Furqan Sehgal
  • 4,917
  • 33
  • 108
  • 167
  • So, the code you've shown works to enumerate all the drive letters, and you're asking how to determine which one is the CD-ROM drive? What do you propose to do in cases where the computer has *multiple* CD drives (such as a CD-RW and a DVD)? – Cody Gray - on strike Mar 12 '11 at 07:18
  • yes sir, that is the issue. May be it can put the letters of all these dirves into a listbox?????? But how to determine the type? – Furqan Sehgal Mar 12 '11 at 07:22

2 Answers2

4

Tested, and returns the correct results on my computer:

Dim cdDrives = From d In IO.DriveInfo.GetDrives() _
                Where d.DriveType = IO.DriveType.CDRom _
                Select d

For Each drive In cdDrives
    Console.WriteLine(drive.Name)
Next

Assumes 3.5, of course, since it's using LINQ. To populate the list box, change the Console.WriteLine to ListBox.Items.Add.

Matt Sieker
  • 9,349
  • 2
  • 25
  • 43
1
For Each drive In DriveInfo.GetDrives()

   If drive.DriveType = DriveType.CDRom Then
       MessageBox.Show(drive.ToString())
   Else 
       MessageBox.Show("Not the cd or dvd rom" & " " & drive.ToString())
   End If

Next
sloth
  • 99,095
  • 21
  • 171
  • 219
Darren
  • 11
  • 1