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

LINQ を使用しているため、もちろん 3.5 を想定しています。リスト ボックスに入力するには、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 に答える