views:

924

answers:

3

Hello, I'm looking for a C# code snippet to give me a notification for when a USB drive is plugged into my PC in Windows Vista (or win7)

+8  A: 

There's a class called DriveDetector over at Codeproject that sounds like it does what you want.

R. Bemrose
+2  A: 

Look at WM_DEVICECHANGE. This message should be sent when a drive is inserted.

mjmarsh
A: 

I know this is not exactly what you asked for, but if you want to check what devices are available at any moment in time, there's another way. You can use the WMI classes in the System.Management namespace, I've used this method and it works well.

If you take a look at this answer, it shows how to enumerate all the drives on the system. You can then take a look at the drive type to determine whether or not it's a USB stick.

Matt Warren