Im currently doing an application for my school project. How can i make the application when the user insert the USB drive into usb port, the login form will automatically pop out asking user to login first and if success, user can use the USB drive(usb port will be enabled). Im using windows 7,vb.net 2010, my application name is PutLock and this app will be installed in drive C. Thanks ^^
Asked
Active
Viewed 1,979 times
0
-
1You could use code to detect if USB are connected and go from there: [link](http://stackoverflow.com/questions/23225170/failed-to-detect-usb) – Cal-cium Sep 21 '16 at 08:17
-
1This article is an interesting read, which might be of some help: [link](http://www.samlogic.net/articles/autorun-usb-flash-drive.htm) – Cal-cium Sep 21 '16 at 08:27
1 Answers
0
I found this solution on internet, THIS CODE IS NOT MINE! But damn, it works really well:
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = &H8000
Private Const DBT_DEVTYP_VOLUME As Integer = &H2
'Device information structure
Public Structure DEV_BROADCAST_HDR
Public dbch_size As Int32
Public dbch_devicetype As Int32
Public dbch_reserved As Int32
End Structure
'Volume information Structure
Private Structure DEV_BROADCAST_VOLUME
Public dbcv_size As Int32
Public dbcv_devicetype As Int32
Public dbcv_reserved As Int32
Public dbcv_unitmask As Int32
Public dbcv_flags As Int16
End Structure
'Function that gets the drive letter from the unit mask
Private Function GetDriveLetterFromMask(ByRef Unit As Int32) As Char
For i As Integer = 0 To 25
If Unit = (2 ^ i) Then
Return Chr(Asc("A") + i)
End If
Next
End Function
'Override message processing to check for the DEVICECHANGE message
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
If CInt(m.WParam) = DBT_DEVICEARRIVAL Then
Dim DeviceInfo As DEV_BROADCAST_HDR
DeviceInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
Dim Volume As DEV_BROADCAST_VOLUME
Volume = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
If IO.File.Exists(IO.Path.Combine(DriveLetter, "test.txt")) Then
'<<<< The test file has been found >>>>
MessageBox.Show("Found test file")
Else
'<<<< Test file has not been found >>>>
MessageBox.Show("Could not find test file")
End If
End If
End If
End If
'Process all other messages as normal
MyBase.WndProc(m)
End Sub
End Class

Rovo93
- 187
- 1
- 4
-
Obviously here the program check for an existing "test" file on the insert USB Drive.. you need to change your code for your needed – Rovo93 Sep 22 '16 at 09:14
-