[英]Getting CD drive letter in 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
測試,並在我的計算機上返回正確的結果:
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。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.