WMI 和 Win32_DeviceChangeEvent - 返回了错误的事件类型?
我正在尝试使用 WMI 注册“设备添加/设备删除”事件。 当我说设备时 - 我的意思是 Disk-On-Key 或任何其他带有我可以访问的文件的设备中的某些东西...
我正在注册该事件,并且引发该事件,但是 EventType财产与我期望看到的不同。
文档(MSDN)指出:1-配置更改,2- 添加设备,3- 删除设备 4- 对接。 由于某种原因,我总是得到 1 的值。
有什么想法吗?
这是示例代码:
public class WMIReceiveEvent
{
public WMIReceiveEvent()
{
try
{
WqlEventQuery query = new WqlEventQuery(
"SELECT * FROM Win32_DeviceChangeEvent");
ManagementEventWatcher watcher = new ManagementEventWatcher(query);
Console.WriteLine("Waiting for an event...");
watcher.EventArrived +=
new EventArrivedEventHandler(
HandleEvent);
// Start listening for events
watcher.Start();
// Do something while waiting for events
System.Threading.Thread.Sleep(10000);
// Stop listening for events
watcher.Stop();
return;
}
catch(ManagementException err)
{
MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
}
}
private void HandleEvent(object sender,
EventArrivedEventArgs e)
{
Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
}
public static void Main()
{
WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
return;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
哦! 是的,我已经经历过这种情况,但前段时间使用原始 Windows API 调用,同时开发了一个检测任何类型媒体插入的 ActiveX 控件。 我将尝试从我的备份中挖掘代码,看看是否可以告诉您我是如何解决它的。 我会订阅 RSS,以防万一有人先到达那里。
Oh! Yup, I've been through that, but using the raw Windows API calls some time ago, while developing an ActiveX control that detected the insertion of any kind of media. I'll try to unearth the code from my backups and see if I can tell you how I solved it. I'll subscribe to the RSS just in case somebody gets there first.
好吧,
你可以尝试win32_逻辑磁盘类并将其绑定到__Instancecreationevent。
您可以轻松获取所需信息
Well,
u can try win32_logical disk class and bind it to the __Instancecreationevent.
You can easily get the required info
我在我的系统上尝试了这个,最终得到了正确的代码。 只需要一段时间。 我收到大约十几个事件,其中之一是设备连接代码。
I tried this on my system and I eventually get the right code. It just takes a while. I get a dozen or so events, and one of them is the device connect code.
嗯,我找不到代码。 尝试了我的旧 RAC 帐户,没有任何结果。 我的旧备份中没有任何内容。 去搞清楚。 但我尝试弄清楚我是如何做到的,并且我认为这是正确的顺序(我的很多内容都基于此 文章):
他们。
消息,并启动一个计时器
1 秒超时(这样做是为了
避免很多杂散
启动 WM_DEVICECHANGE 消息
一旦插入就开始
USB 密钥/其他设备仅结束
当驱动器“稳定”时)。
旧缓存并检测新缓存。
我知道还有其他方法,但事实证明这是唯一可以在不同版本的 Windows 中一致工作的方法,并且我们需要这种方法,因为我的客户在网页上使用 ActiveX 控件,该控件从您插入的任何类型的设备上传图像(我认为他们生产了某种打印亭)。
Well, I couldn't find the code. Tried on my old RAC account, nothing. Nothing in my old backups. Go figure. But I tried to work out how I did it, and I think this is the correct sequence (I based a lot of it on this article):
them.
message, and start a timer with a
timeout of 1 second (this is done to
avoid a lot of spurious
WM_DEVICECHANGE messages that start
as start as soon as you insert the
USB key/other device and only end
when the drive is "settled").
old cache and detect the new ones.
I know there are other methods, but that proved to be the only one that would work consistently in different versions of windows, and we needed that as my client used the ActiveX control on a webpage that uploaded images from any kind of device you inserted (I think they produced some kind of printing kiosk).