georgemackenzie georgemackenzie - 1 year ago 110 Question

Restrict Drive Letter Selection By Type VB.NET

I have a form on which a user can select a source drive letter:

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

I need to restrict the selection of drive letters to CDROM or USB. My code below validates CDROM drive letters but not USB drive letters:

' 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

How do I configure the code above to validate the drive letter selected is CDROM or USB?


George :-)


You are just missing brackets on the condition:

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

Put simply, you had:

If Not A Or B

But the Not does not apply to B without the brackets - it only applies to A.