在VB.NET中获取CD驱动器号

时间:2011-03-12 07:13:43

标签: .net windows vb.net drives drive-letter

我使用以下代码获取计算机上每个驱动器的字母列表。我想从这个列表中获取CD驱动器的驱动器号。我该如何检查?

我用来获取列表的代码如下:

Form.Load事件中:

    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

2 个答案:

答案 0 :(得分:4)

经过测试,并在我的计算机上返回正确的结果:

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

当然,假设3.5,因为它使用的是LINQ。要填充列表框,请将Console.WriteLine更改为ListBox.Items.Add。

答案 1 :(得分: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