[英]C# USB removal event in Windows XP
I am developing a C# class library for a USB-Serial device. 我正在为USB串行设备开发C#类库。 I need an event to know when is the USB being removed.
我需要一个事件来知道何时删除USB。
Currently I am using WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3")
to get notified when the USB is being removed, so that I can disconnect the port after it is unplugged. 当前,我正在使用
WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3")
来在删除USB时收到通知,以便在拔下端口后可以断开端口。
This works fine in Windows 7, windows 8 and windows 10. But in Windows XP SP3, the event does not get triggered when the USB is being removed. 在Windows 7,Windows 8和Windows 10中可以正常工作。但是在Windows XP SP3中,删除USB时不会触发该事件。 (Remark: My windows XP is installed with .NET 3.5 and .NET 3.5 SP1)
(备注:我的Windows XP已安装.NET 3.5和.NET 3.5 SP1)
Is there a way I able to get notified when the USB is being removed in Windows XP? 在Windows XP中删除USB时,是否可以通过某种方式得到通知?
I found the solution ! 我找到了解决方案!
string query = "SELECT * FROM __InstanceDeletionEvent WITHIN [interval] WHERE TargetInstance ISA 'Win32_USBHub'";
var watcher = new ManagementEventWatcher(query);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();
Remark: Replace [interval] to specify a polling interval, which you may get more information HERE . 备注:替换[interval]以指定轮询间隔,您可以在此处获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.