Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0 VB9.0 VB VB10.0 VB.NET , +
Show that a usb device is connected to the pc in vb.net/
Posted 14-Feb-13 3:31am
Comments
Sandeep Mewara at 14-Feb-13 8:55am
   
Why posting a question and then 3 answers for it and marking it as answer too? If you found solution to your problem, you can add all in one and mark that as an answer - not 3 different posts!
Richard MacCutchan at 14-Feb-13 9:48am
   
Please do not do this. If you have some useful information that you wish to share then post it as an Article or Tip.
Maciej Los at 14-Feb-13 12:13pm
   
Good advice. My virtual 5!
virajdaw at 15-Feb-13 5:03am
   
sorry, i'm new.i didn't knew about this...
won't do in future//
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Public Class Form1
 
    Private Const WM_DEVICECHANGE As Integer = &H219
    Private Const DBT_DEVICEARRIVAL As Integer = 32768
 
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            If m.WParam = DBT_DEVICEARRIVAL Then
                MessageBox.Show("Removable Drive Detected")
            End If
        End If
        MyBase.WndProc(m)
    End Sub
End Class
 
[Moved from Solution 3 by OP]
VB.NET – Detecting when a USB drive is connected and getting the drive letter
 
The example code below will detect when a USB drive is connected and will then look for a file named test.txt in the USB drive.
 
Go the following link
http://blog.cjwdev.co.uk/2009/11/10/vb-net-detecting-when-a-usb-drive-is-connected-and-getting-the-drive-letter/[^]
 
[Moved from Solution 4 by OP]
http://www.vbforums.com/showthread.php?t=534956[^]
 
[Moved from Solution 5 by OP]
[NOTE: The OP has failed to cite the source for this code which is here.]
Imports System.Runtime.InteropServices
Public Class Form1
    'Used to detected if any of the messages are any of these constants values.
    Private Const WM_DEVICECHANGE As Integer = &H219
    Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004
    Private Const DBT_DEVTYP_VOLUME As Integer = &H2  '
    '
    'Get the information about the detected volume.
    Private Structure DEV_BROADCAST_VOLUME
        Dim Dbcv_Size As Integer
        Dim Dbcv_Devicetype As Integer
        Dim Dbcv_Reserved As Integer
        Dim Dbcv_Unitmask As Integer
        Dim Dbcv_Flags As Short
    End Structure
 
    Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
        '
        'These are the required subclassing codes for detecting device based removal and arrival.
        '
        If M.Msg = WM_DEVICECHANGE Then
            Select Case M.WParam
                '
                'Check if a device was added.
                Case DBT_DEVICEARRIVAL
                    Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                    If DevType = DBT_DEVTYP_VOLUME Then
                        Dim Vol As New DEV_BROADCAST_VOLUME
                        Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                        If Vol.Dbcv_Flags = 0 Then
                            For i As Integer = 0 To 20
                                If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                    Dim Usb As String = Chr(65 + i) + ":\"
                                    MsgBox("Looks like a USB device was plugged in!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)
                                    Exit For
                                End If
                            Next
                        End If
                    End If
                    '
                    'Check if the message was for the removal of a device.
                Case DBT_DEVICEREMOVECOMPLETE
                    Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
                    If DevType = DBT_DEVTYP_VOLUME Then
                        Dim Vol As New DEV_BROADCAST_VOLUME
                        Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
                        If Vol.Dbcv_Flags = 0 Then
                            For i As Integer = 0 To 20
                                If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
                                    Dim Usb As String = Chr(65 + i) + ":\"
                                    MsgBox("Looks like a volume device was removed!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)
                                    Exit For
                                End If
                            Next
                        End If
                    End If
            End Select
        End If
        MyBase.WndProc(M)
    End Sub
End Class
I've just tested this code in VB.net 2010. It works perfectly!
  Permalink  
v4
Comments
Marcus Kramer at 14-Feb-13 10:05am
   
In the future, please just post 1 answer and when you feel the need to add more information use the Improve Solution widget on that solution rather than adding a bunch of extra solutions. Cheers.
Marcus Kramer at 14-Feb-13 10:06am
   
Also: Always cite your source. You did not write the code in this solution so you should provide a link to where it came from.
In this case: Source
Richard MacCutchan at 14-Feb-13 12:28pm
   
In that case this post should really be deleted in its entirety.
Marcus Kramer at 14-Feb-13 12:38pm
   
The answer is not bad and it was a first offence so I figured a warning and adding the link would suffice on this one.
Ajai Kumar V.S at 3-Feb-15 9:50am
   
It doesn't work can you attach a executable file
CHill60 at 3-Feb-15 10:00am
   
No, there is no mechanism for attaching executables in this forum.
The code does work - what problem are you having?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Doesn't work. Where should i place these codes??
  Permalink  
Comments
CHill60 at 3-Feb-15 9:57am
   
If you want to respond to a specific post then use the "Have a Question or Comment" link next to it. Don't post questions as solutions

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 484
1 Peter Leow 443
2 Sergey Alexandrovich Kryukov 288
3 Kornfeld Eliyahu Peter 270
4 Maciej Los 230


Advertise | Privacy | Mobile
Web01 | 2.8.150301.1 | Last Updated 3 Feb 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100