2011-03-12 40 views
4

我正在使用以下代碼來獲取我的計算機上每個驅動器的字母列表。我想從此列表中獲取CD驅動器的驅動器號。我如何檢查它?在VB.NET中獲取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 
+0

因此,您所展示的代碼可用於枚舉所有驅動器號,並且您問如何確定哪一個是CD-ROM驅動器?如果計算機有多個CD驅動器(如CD-RW和DVD),您打算如何處理? – 2011-03-12 07:18:50

+0

是的,先生,這是問題。也許它可以把所有這些dirves的信件放入一個listbox ??????但如何確定類型? – 2011-03-12 07:22:37

回答

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。

+0

Hrmm,看起來像我* *想念它。好答案。 – 2011-03-12 07:47:23

+0

偉大的答案!工作正常。謝謝 – 2011-03-12 10:02:12

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