Event when removable drived added/removed

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

Is there an event that I can subscribe to, to get notified when a removable
drive is added or removed?

Thanks
LS
 
Is there an event that I can subscribe to, to get notified when a
removable drive is added or removed?

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! |
*******************************************
 
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! |
*******************************************

Thanks for the guidance. Guess I should have said I was using WPF. I am
trying to use the code you pointed out in conjunction with some code I found
for overriding the winproc in WPF. So far no joy.

Thanks
LS
 
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
 
Lloyd Sheen said:
Thanks for the guidance. Guess I should have said I was using WPF.

Probably, because most of use get myopic when there is no context. LOL

Peace and Grace,

--
Gregory A. Beamer (MVP)

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top