I'm writing a small application to detect the insertion of USB memory sticks
and prompt the user to accept the device or reject it. Rejecting it would
prevent its being loaded by Windows.
I have the code that detects insertion & removal working fine but have no
idea how to cancel the insertion.
I was thinking that I could intercept the DEVICEARRIVAL message and kill it
off before its actioned by Windows but I am lost with this.
Has anyone got and ideas on this. I'd really appreciate some sample vb.net
code to show the way.
Heres the main part of the code I have so far-
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
'This kills off any 'autoplay' capability in the stick
If QueryCancelAutoPlay = 0 Then
QueryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay")
If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
lblMessage.Text = "USB Removed"
ElseIf m.Msg = QueryCancelAutoPlay Then
m.Result = CType(1, IntPtr)
'Presumably I can insert a different message to cancel here??
Private Sub ProcessUSB()
For Each ThisDrive As System.IO.DriveInfo In My.Computer.FileSystem.
If ThisDrive.DriveType.ToString = "Removable" Then
'Prompt user for accept/reject etc
lblMessage.Text = "USB Inserted"