1

我有一个表单,用户可以在该表单上选择源驱动器号:

If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
    TextBox1.Text = FolderBrowserDialog1.SelectedPath
End If

我需要将驱动器号的选择限制为 CDROM 或 USB。我下面的代码验证 CDROM 驱动器号,但不验证 USB 驱动器号:

' Check selected drive type is CDROM or USB
Dim Drive As New IO.DriveInfo(TextBox1.Text)
If Drive.IsReady = True Then
    If Not Drive.DriveType = IO.DriveType.CDRom or Drive.DriveType = IO.DriveType.Removable Then
    MessageBox.Show("Source folder must be CD/DVD or USB.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Exit Sub
    End If
End If

如何配置上面的代码以验证选择的驱动器号是 CDROM 还是 USB?

4

1 回答 1

2

您只是缺少条件括号:

If Not (Drive.DriveType = IO.DriveType.CDRom or Drive.DriveType = IO.DriveType.Removable) Then

简单地说,你有:

If Not A Or B

Not不适用于没有括号的 B - 它仅适用于 A。

于 2016-06-05T10:44:10.470 回答