Gregory A. Beamer said:
Assume all removables are USB today.
Start here:
http://www.codeproject.com/KB/system/DriveDetector.aspx
This is a wrapper project for the Windows libraries that work with these
drives.
Peace and Grace,
--
Gregory A. Beamer (MVP)
Twitter: @gbworld
Blog:
http://gregorybeamer.spaces.live.com
*******************************************
| Think outside the box! |
*******************************************
For those who may have read the post I will post the code that I came up
with (so far) that I will use to detect the changes.
First in the window event SourceInitialized add the following code:
Private Sub MainWindow_SourceInitialized(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.SourceInitialized
Dim src As HwndSource = HwndSource.FromHwnd(New
WindowInteropHelper(Me).Handle)
src.AddHook(New HwndSourceHook(AddressOf WndProc))
End Sub
Then add the code of the winProc that you wish to handle:
Public Function WndProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal
wParam As IntPtr, ByVal lParam As IntPtr, ByRef handled As Boolean) As
IntPtr
If msg = WM_DEVICECHANGE Then ' you will have to put either a CONST in
your code or hard code this value which is 537
If deviceChanged Is Nothing Then
deviceChanged = New DispatcherTimer
AddHandler deviceChanged.Tick, AddressOf deviceChanged_Tick
deviceChanged.Interval = TimeSpan.FromMilliseconds(1000)
deviceChanged.Start()
Else
deviceChanged.Stop()
deviceChanged.Start()
End If
End If
Return IntPtr.Zero
End Function
I am doing the code in a timer event so that it will not interfer with the
app. In my app this is fine and you can fine tune the dispatch timer
interval if needed. I found that a second was just fine since this event
takes a certain amount of time.
Then there is the dispatchertimer code:
Private deviceChanged As DispatcherTimer
Private Sub deviceChanged_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
deviceChanged.Stop()
'Debug.WriteLine("A device has changed")
' this is where I will put code in to handle the add/remove of a device
deviceChanged = Nothing
End Sub
In docs it says that the wParam should contain different values if the
device is added or removed but I found that the parameter was the same for
each occasion. The wierd code in the wndProc about setting up the timer
occurs because there is about 7 or eight messages sent so I am just handling
the last one.
Thanks again for getting me on the right track and I hope the above code
will help others.
LS