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

不打扰是莪最后的温柔 提交于 2019-11-28 07:34:31

问题


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.


回答1:


A windows service should do the trick

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




回答2:


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.




回答3:


This blogpost show how to handle system events, https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/howdoi/?p=135 maybe you can use it for the event that you want to handle




回答4:


You may create a hidden window or run a service as it should work with a window handle or a service status handle.



来源:https://stackoverflow.com/questions/1783657/get-notification-when-a-new-drive-is-connected-via-usb-or-other-means-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!