4

我正在使用以下代码获取计算机上每个驱动器的字母列表。我想从此列表中获取 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
4

2 回答 2

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。

于 2011-03-12T07:44:45.540 回答
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
于 2011-07-30T04:12:09.613 回答