我有一个表单,用户可以在上面选择源驱动器号:
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?
您只是缺少条件的括号:
If Not (Drive.DriveType = IO.DriveType.CDRom or Drive.DriveType = IO.DriveType.Removable) Then
简而言之,您有:
If Not A Or B
但是Not
不适用于没有括号的 B - 它仅适用于 A。