none
DVD Drive and USB (External Disk) Selection RRS feed

  • Question

  • Hi,

    I am using Forms to and here I am using full path to save the file on USB /external disk (D:\MyUSB\MyFile.pdf). The drive letter in most of the cases is "D", however, recently I have encounter a situation where D is the DVD and hence (D:\MyUSB\MyFile.pdf) didn't worked.

    Can we check for the attached external USB drive assigned letter and save the files into this drive? I mean if type is CD/DVD, ignore it and check for other drive,if its USB, copy the file there instead.

    Sunday, March 22, 2020 9:14 PM

Answers

  • Hi

    Try this

        Dim message As String = Nothing
        Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
        For Each drv As IO.DriveInfo In drvs
          If drv.DriveType = IO.DriveType.Removable Then
            message &= "Drive " & drv.Name & " is a Removable drive" & vbCrLf
          End If
        Next
        If Not message = Nothing Then
          MessageBox.Show(message)
        Else
          MessageBox.Show("No Removable Drives found")
        End If


    Regards Les, Livingston, Scotland

    • Proposed as answer by Cor Ligthert Monday, March 23, 2020 7:13 PM
    • Marked as answer by -OSD- Tuesday, March 24, 2020 11:49 AM
    Monday, March 23, 2020 4:40 PM

All replies

  • Hi

    Is ths link any help?


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, March 23, 2020 12:57 AM
    Sunday, March 22, 2020 9:27 PM
  • Its C#, and 

    Can we check for the attached external USB drive assigned letter and save the files into this drive? I mean if type is CD/DVD, ignore it and check for other drive,if its USB, copy the file there instead.

    • Edited by -OSD- Monday, March 23, 2020 8:40 AM
    Monday, March 23, 2020 8:39 AM
  • Its C#, and ...

    Change the Language to VB.NET on Top-Right...
    Monday, March 23, 2020 8:51 AM
  • Its C# [...]


    There is a Language drop-down list on the page where you can select “VB”.

    But it is probably better to ask the user, because the system could have several USB drives. In order to list and filter the drives and present your own drop-down list to the user, see the suggested DriveInfo class and DriveType property.

    Maybe something like this:

    Dim removable_directories As String() =

       DriveInfo.GetDrives _

          .Where(Function(d) d.DriveType = DriveType.Removable) _

          .Select(Function(d) d.RootDirectory.FullName) _

          .ToArray

     

    If it is suitable, you can take the first element (removable_directories(0)) and append the file path.



    • Edited by Viorel_MVP Monday, March 23, 2020 9:04 AM
    Monday, March 23, 2020 9:01 AM
  • Maybe I should ask in this way instead:

    How to write to "D Drive" ONLY if it's USB and not a DVD.

    Monday, March 23, 2020 1:23 PM
  • Maybe I should ask in this way instead:

    How to write to "D Drive" ONLY if it's USB and not a DVD.

    Hi

    Are you unable to form code to interegate the information returned from a call to

    DriveInfo.GetDrives?


    Regards Les, Livingston, Scotland

    Monday, March 23, 2020 2:31 PM
  • Not yet, but eventually will figure it out, thanks anyways.
    Monday, March 23, 2020 2:48 PM
  • Not yet, but eventually will figure it out, thanks anyways.

    Hi

    Here would be my attempt. As I only have an internal fixed SSD I can't test fully.

        Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
        Dim drv As IO.DriveInfo
        For Each drv In drvs
          ' set the parameters to suit
          ' eg  D:\   and 
          ' IO.DriveType.Removable
          If drv.Name = "C:\" AndAlso drv.DriveType = IO.DriveType.Fixed Then
            ' here we have met the
            ' restricions. In my case,
            ' my innternal fixed disk
            Stop
          End If
        Next


    Regards Les, Livingston, Scotland

    Monday, March 23, 2020 3:09 PM
  • I guess it is not easy to do because it is in 2 sections of the win32 management info 

    Here a link from Ray (IronRazerz) which gives the right direction. 

    A pity is that with win32 I did not get the drive letter in one time, so it will be not direct easy. 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/87f09ece-0b4c-4ea6-b54c-ae31983aabc5/find-usb-divices-serial-number?forum=vbgeneral

    You find the usb in Hardware, Mass Storage, DiskDrive with the tool which is in the link 


    Success
    Cor

    Monday, March 23, 2020 4:14 PM
  • Not yet, but eventually will figure it out, thanks anyways.

    Hi

    Here would be my attempt. As I only have an internal fixed SSD I can't test fully.

        Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
        Dim drv As IO.DriveInfo
        For Each drv In drvs
          ' set the parameters to suit
          ' eg  D:\   and 
          ' IO.DriveType.Removable
          If drv.Name = "C:\" AndAlso drv.DriveType = IO.DriveType.Fixed Then
            ' here we have met the
            ' restricions. In my case,
            ' my innternal fixed disk
            Stop
          End If
        Next


    Regards Les, Livingston, Scotland

    Almost done, thanks for example,

    However, when I write the ELSE, to show message that there is no USB stick attached, I get the message box thrice.

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim drv As IO.DriveInfo
            For Each drv In drvs
                ' IO.DriveType.Removable
                ' IO.DriveType.CDRom
                ' IO.DriveType.Fixed
                If drv.Name = "D:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
    
                    MsgBox("D is the USB Disk")
                ElseIf drv.Name = "E:\" AndAlso drv.DriveType = IO.DriveType.Removable Then  'System has DVD drive and letter "D" is not assigned to USB Disk, check for E drive.
    
                    MsgBox("E is the USB Disk")
                ElseIf drv.Name = "F:\" AndAlso drv.DriveType = IO.DriveType.Removable Then   'System has E drive and letter "E" is not assigned to USB Disk, check for F drive.
    
                    MsgBox("F is the USB Disk")
    
                Else MsgBox("There is no USB disk found on this system.")
    
                End If
            Next
        End Sub
    End Class

    Am I using Else statement in a wrong way?

    Monday, March 23, 2020 4:23 PM
  • Hi

    Try this

        Dim message As String = Nothing
        Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
        For Each drv As IO.DriveInfo In drvs
          If drv.DriveType = IO.DriveType.Removable Then
            message &= "Drive " & drv.Name & " is a Removable drive" & vbCrLf
          End If
        Next
        If Not message = Nothing Then
          MessageBox.Show(message)
        Else
          MessageBox.Show("No Removable Drives found")
        End If


    Regards Les, Livingston, Scotland

    • Proposed as answer by Cor Ligthert Monday, March 23, 2020 7:13 PM
    • Marked as answer by -OSD- Tuesday, March 24, 2020 11:49 AM
    Monday, March 23, 2020 4:40 PM
  • I tested the last code from Les and I think that it is the best you can get. 

    I don't know anymore another removable drive than the USB.

    An SD disk maybe but then that should be inserted so a low chance that'll be a problem.


    Success
    Cor

    Monday, March 23, 2020 7:15 PM
  • Hi

    Try this

        Dim message As String = Nothing
        Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
        For Each drv As IO.DriveInfo In drvs
          If drv.DriveType = IO.DriveType.Removable Then
            message &= "Drive " & drv.Name & " is a Removable drive" & vbCrLf
          End If
        Next
        If Not message = Nothing Then
          MessageBox.Show(message)
        Else
          MessageBox.Show("No Removable Drives found")
        End If


    Regards Les, Livingston, Scotland

    Thanks, it worked as intended.
    Tuesday, March 24, 2020 11:49 AM