按类型限制驱动器号选择

Restrict Drive Letter Selection By Type

我有一个表单,用户可以在其中 select 源驱动器盘符:

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

我需要限制 select驱动器盘符只能访问 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

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

您只是缺少条件括号:

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

简而言之,您有:

If Not A Or B

但是 Not 不适用于没有括号的 B - 它只适用于 A。