ajma
ajma

Reputation: 12206

Looking for C# code for detecting removable drive (usb flash)

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)

Upvotes: 5

Views: 5122

Answers (3)

Matt Warren
Matt Warren

Reputation: 10291

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.

Upvotes: 1

Powerlord
Powerlord

Reputation: 88786

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

Upvotes: 12

Mike Marshall
Mike Marshall

Reputation: 7850

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

Upvotes: 3

Related Questions