none
Removable Device Detection and File Copy RRS feed

  • Question

  • Hi,

    Trying to solve a very simple scenario where VB Forms applications should detect if the the attached disk /drive is the USB and if yes, copy some contents into this otherwise inform the user that no USB drive found. 

    Here is what I am doing, but seems like I have not desired results as "D" dive is fixed drive but application consider this as removable and never checks for the "E" drive.

    Imports System.IO
    
    Public Class Form1
    
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            For Each drive As IO.DriveInfo In My.Computer.FileSystem.Drives
                If IO.Directory.Exists("D:\") And IO.DriveType.Removable Then
                    My.Computer.FileSystem.CopyFile("C:\Windows\Temp.txt", "D:\Temp.txt")
                ElseIf IO.Directory.Exists("E:\") And IO.DriveType.Removable Then
                    My.Computer.FileSystem.CopyFile("C:\Windows\Temp.txt", "E:\Temp.txt")
                Else
                    MessageBox.Show("No USB device found.")
                End If
            Next
        End Sub
    End Class

    Thursday, April 2, 2020 12:38 PM

Answers

  • I hope that you are able to tailor from sample code to this yourself. 

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Checking the USB Drive'
            Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim usbdrive = ""
            For Each drv In drvs
                If drv.DriveType = IO.DriveType.Removable Then
                    MessageBox.Show("I am " & drv.Name & " Drive, a USB Stick")
                    usbdrive = drv.Name
                End If
            Next
            If usbdrive = "" Then MessageBox.Show("no Usb drive inserted")
    The driveletter is then in the usbdrive variable. 


    Success
    Cor



    • Edited by Cor Ligthert Friday, April 3, 2020 9:52 AM
    • Marked as answer by -OSD- Friday, April 3, 2020 1:24 PM
    Friday, April 3, 2020 9:51 AM

All replies

  • There are 3 things wrong in your code. 

    First of all, you are iterating through the drives. The second time you find that the drive is removable but as well that the D drive does exist. And therefore the first if is true, and writes to the D drive. 

    The second wrong thing but that does not affect the use of the Boolean operator And, in your situation it has to be AndAlso 

    The third thing is that you make from the code from Les a kind of spaghetti with an ElseIf in it and therefore difficult to investigate. You show that you did not even understand it yourself. Therefore avoid ElseIF

    Here the code from Les, there is nothing needed to change that code to fulfil your problem. 

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/3b49072f-ba9c-4ccb-badf-73dc7f348feb/dvd-drive-and-usb-external-disk-selection?forum=vbgeneral#8563259d-6aae-47ba-b08d-6e0a37eec00c


    Success
    Cor

    Thursday, April 2, 2020 1:25 PM
  • So you mean it does not make sense to check the "D" drive if its available and then check its type? And if type is not correct, check the drive "E" to determine if this is the USB drive?
    • Edited by -OSD- Thursday, April 2, 2020 1:36 PM
    Thursday, April 2, 2020 1:32 PM
  • So you mean it does not make sense to check the "D" drive if its available and then check its type? And if type is not correct, check the drive "E" to determine if this is the USB drive?

    But that is not what you are doing. 

    You check if there is a removable drive and then checks if there is a D drive available and write to the D drive. 


    Success
    Cor

    Thursday, April 2, 2020 1:46 PM
  • You check if there is a removable drive and then checks if there is a D drive available and write to the D drive.

    Correct, I am trying to achieve this: with addition to if D is not available, check for E or F that's why ifElse, do you have better idea?

    Thursday, April 2, 2020 1:54 PM
  • You check if there is a removable drive and then checks if there is a D drive available and write to the D drive.

    Correct, I am trying to achieve this: with addition to if D is not available, check for E or F that's why ifElse, do you have better idea?

    No that is not what you are doing. What you do is:

    Check in a loop from the first drive to the last drive if D is available and if it is check then check also if the current drive in the loop is removeable, if there is no D drive then check if there is an E drive which is removable. But that will never be done. Because there is always a D drive (in your case) and that ElseIf will never be reached.

    That is why I wrote avoid programming with ElseIf because many persons don't understand that it is often never reached and if they are elseif's take only longer to be done. 

    Be aware giving you the code is for me less difficult then trying to teach you how to do these things right. 


    Success
    Cor


    Thursday, April 2, 2020 3:07 PM
  • Hi -OSD-,

    Thank you for posting here.

    Based on my research, you can refer to the following code to check the removable disk and copy your file:

                Using mcDriveClass As ManagementClass = New ManagementClass("Win32_LogicalDisk")
                Using mocDrives As ManagementObjectCollection = mcDriveClass.GetInstances()
                    For Each moDrive As ManagementObject In mocDrives
                        Try
                            If moDrive.Properties("Description").Value.ToString() = "Removable Disk" Then
                                'copy file
                            End If
                        Catch
                        End Try
                    Next
                End Using
            End Using

    Besides, if you want to copy file while USB is inserted, here's the code you can refer to:

        Sub Main()
            Dim q_creation As WqlEventQuery = New WqlEventQuery()
            q_creation.EventClassName = "__InstanceCreationEvent"
            q_creation.WithinInterval = New TimeSpan(0, 0, 2)
            q_creation.Condition = "TargetInstance ISA 'Win32_USBHub'"
            Dim mwe_creation As ManagementEventWatcher = New ManagementEventWatcher(q_creation)
            AddHandler mwe_creation.EventArrived, AddressOf USBEventArrived_Creation
            mwe_creation.Start()
    
            Console.ReadLine()
        End Sub
        Private Sub USBEventArrived_Creation(ByVal sender As Object, ByVal e As EventArrivedEventArgs)
            ' USB is inserted into the computer, and then you can copy your file to the USB.
        End Sub

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 3, 2020 7:02 AM
    Moderator
  • Ok, the following code works at the time being:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ''Checking the USB Drive
            Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim drv As IO.DriveInfo
            For Each drv In drvs
                If drv.Name = "D:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                    MessageBox.Show("I am D Drive, a USB Stick")
                Else
                    If drv.Name = "E:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                        MessageBox.Show("I am E Drive, a USB Stick")
                    Else
                        If drv.Name = "F:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                            MessageBox.Show("I am F Drive, a USB Stick")
    
                        End If
                    End If
                End If
            Next
            ''End Checking USB Drive 
        End Sub
    End Class

    But when I add another Else, to inform user no USB found, I am getting this message even USB is present:

     Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim drv As IO.DriveInfo
            For Each drv In drvs
                If drv.Name = "D:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                    MessageBox.Show("I am D Drive, a USB Stick")
                Else
                    If drv.Name = "E:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                        MessageBox.Show("I am E Drive, a USB Stick")
                    Else
                        If drv.Name = "F:\" AndAlso drv.DriveType = IO.DriveType.Removable Then
                            MessageBox.Show("I am F Drive, a USB Stick")
                        Else
                            MessageBox.Show("No USB device found")
                        End If
                    End If
                End If
            Next

    Friday, April 3, 2020 8:15 AM
  • Yes looks much better, but why you like so much "else" statements. There is no need for.

    In my perception is this code, which I made from yours, intentionally the same

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ''Checking the USB Drive
            Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim usbdrive = False
            For Each drv In drvs
                If drv.DriveType = IO.DriveType.Removable Then
                    MessageBox.Show("I am " & drv.Name & " Drive, a USB Stick")
                    usbdrive = True
                End If
            Next
            If Not usbdrive Then MessageBox.Show("no Usb drive inserted")
        End Sub
    End Class
    However, maybe the USB drive can now even be inserted in drive K. 

     

    Success
    Cor

    Friday, April 3, 2020 9:12 AM
  • The target is to determine the USB disk and then copy a file to USB disk (which requires to know the USB drive letter). As I have in my first post above.
    • Edited by -OSD- Friday, April 3, 2020 9:36 AM
    Friday, April 3, 2020 9:15 AM
  • I hope that you are able to tailor from sample code to this yourself. 

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Checking the USB Drive'
            Dim drvs() As IO.DriveInfo = IO.DriveInfo.GetDrives()
            Dim usbdrive = ""
            For Each drv In drvs
                If drv.DriveType = IO.DriveType.Removable Then
                    MessageBox.Show("I am " & drv.Name & " Drive, a USB Stick")
                    usbdrive = drv.Name
                End If
            Next
            If usbdrive = "" Then MessageBox.Show("no Usb drive inserted")
    The driveletter is then in the usbdrive variable. 


    Success
    Cor



    • Edited by Cor Ligthert Friday, April 3, 2020 9:52 AM
    • Marked as answer by -OSD- Friday, April 3, 2020 1:24 PM
    Friday, April 3, 2020 9:51 AM
  • Interesting, will check that out.
    Friday, April 3, 2020 10:14 AM
  • Manged it to copy to USB drive and show message in case no USB was present.

    Learning: Can we change the order of the following statement before the message box?

    usbdrive = drv.Name

    For example, this code (in testing it worked fine but need to understand the logic).

    If drv.DriveType = IO.DriveType.Removable Then
                    usbdrive = drv.Name
                    MessageBox.Show("I am " & drv.Name & " Drive, a USB Stick")
                     My.Computer.FileSystem.CopyFile("C:\Windows\Temp.txt", usbdrive & "Test.txt")
                End If
    
    


    Friday, April 3, 2020 12:57 PM
  • This kind of things you would simply try. 

    It has no effect. I would prefer

    If drv.DriveType = IO.DriveType.Removable Then
                    usbdrive = drv.Name
                    MessageBox.Show("I am " & drv.Name & " Drive, a USB Stick")
                     My.Computer.FileSystem.CopyFile("C:\Windows\Temp.txt", drv.Name & "Test.txt")
                End If

    The reason, that usbdrive variant can becomes just a Boolean value to tell that it is  what makes it more easier to read after 5 years you see it again when you've to maintain this. 

    But it is not real important, just my preference. 


    Success
    Cor



    Friday, April 3, 2020 1:08 PM
  • I have marked it as Answer though your last post didn't understand :)
    Friday, April 3, 2020 1:25 PM