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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5281202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:25:18  来源:igfitidea点击:

Getting CD drive letter in VB.NET

.netwindowsvb.netdrivesdrive-letter

提问by Furqan Sehgal

I am using the following code to get a list of the letters for each drive on my computer. I want to get the drive letter of the CD drive from this list. How do I check it?

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

The code I am using to get list is as below:

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

In the Form.Loadevent:

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

采纳答案by Matt Sieker

Tested, and returns the correct results on my computer:

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

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

Assumes 3.5, of course, since it's using LINQ. To populate the list box, change the Console.WriteLine to ListBox.Items.Add.

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

回答by Darren

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