views:

2006

answers:

4

Looking into possibility of making an USB distributed application
that will autostart on insertion of an USB stick and shutdown when removing the stick

Will use .Net and C#.
Looking for suggestion how to approach this using C#?


Update: Two possible solutions implementing this as a service.
- override WndProc
or
- using WMI query with ManagementEventWatcher

+3  A: 

There's an article on codeproject.

Darin Dimitrov
Beat me to linking it. Good article.
Ben S
Good article +1.
Kb
+1  A: 

Try WM_CHANGEDEVICE handling.

mjmarsh
+2  A: 

You can also use WMI to detect insertion events. It's a little bit more complicated than monitoring for WM_CHANGEDEVICE messages, but it does not require a window handle which may be useful if you are running in the background as a service.

John Conrad
@John Conrad: +1 WMI is a good choice. Also found a SO topic on this:http://stackoverflow.com/questions/39704/wmi-and-win32devicechangeevent-wrong-event-type-returned
Kb
Actually WMI is much simpler solution. I'm posting it below as another solution.
VitalyB
+2  A: 

You can use WMI, it is easy and it works a lot better than WndProc solution with services.

Here is a simple example:

ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();

And that's it :)

VitalyB