Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get notification when a new drive is connected via USB or other means (C#)

I need to detect when a new drive is plugged into a Windows system through a USB port, firewire port, etc. I am aware of the WM_DEVICECHANGE message, but this requires a window to be open in order to use it. Is there a way to create a background process in C# that detects when new drives are plugged in?

Background info: I am wanting to write this app to remove a worm from company removable drives. Regular antivirus software is failing to removing it. We have a script that will remove the worm, but it must be run manually, which is not good enough to contain the outbreak.

like image 454
Phil Avatar asked Nov 23 '09 15:11

Phil


2 Answers

A windows service should do the trick

Try reworking the CodeProject DriveDetector as a service and you should have what you need

like image 124
Chris Ballance Avatar answered Sep 28 '22 16:09

Chris Ballance


You will need to subscribe to RegisterDeviceNotification, and bind it to a service status handle. See also http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ae4478a-1133-44ba-8a1c-3e4be3856e37.

I'm not quite sure how to implement this, has been a long time ago.

like image 31
Jan Jongboom Avatar answered Sep 28 '22 15:09

Jan Jongboom