按类型限制驱动器号选择

时间:2016-06-05 10:38:28

标签: vb.net drives drive-letter

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

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?

1 个答案:

答案 0 :(得分:2)

你只是在条件上缺少括号:

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

简单地说,你有:

If Not A Or B

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