在.NET应用程序中使用RegisterDeviceNotification应用程序、NET、RegisterDeviceNotification

2023-09-03 21:06:27 作者:孤独你走吧,我有寂寞陪我

我已经看到了关于如何使用RegisterDeviceNotification从Windows API的一些例子,但我还没有看到任何.NET的例子。我想写一个C#应用程序的新驱动器出现时被通知(尤其是通过USB,火线等)。这个程序需要一个Windows服务,所以没有一些不好的做法黑客无法使用WM_DEVICECHANGE消息。我想避免这种情况。谁能给我如何使用RegisterDeviceNotification在C#中,或者至少给我一个很好的选择,它的样本?

I have seen some examples on how to use RegisterDeviceNotification from the Windows API, but I have not seen any .NET examples. I want to write a C# app that is notified when a new drive appears (specifically through USB, firewire, etc). This app needs to be a Windows service, so I can't use WM_DEVICECHANGE messages without some bad-practice hacking. I'd like to avoid that. Can anyone give me a sample of how to use RegisterDeviceNotification in C#, or at least give me a good alternative to it?

推荐答案

这篇文章可以帮助:的捕获设备事件在C#Windows服务

This article might help: Capturing Device Events in a C# Windows Service