WMI和Win32_DeviceChangeEvent - 返回错误的事件类型? [英] WMI and Win32_DeviceChangeEvent - Wrong event type returned?

查看:251
本文介绍了WMI和Win32_DeviceChangeEvent - 返回错误的事件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WMI注册到已添加设备添加/删除设备事件。当我说的设备 - 我的意思是一个磁盘上的钥匙或任何其他设备上有文件,我可以访问...

I am trying to register to a "Device added/ Device removed" event using WMI. When I say device - I mean something in the lines of a Disk-On-Key or any other device that has files on it which I can access...

我的注册该事件,并且事件被提出,但事件类型属性与我期望看到的不同。

I am registering to the event, and the event is raised, but the EventType propery is different from the one I am expecting to see.

文档( MSDN )状态:1-配置更改,2-设备添加, 3设备移除4-对接。由于某种原因,我总是得到一个值1.

The documentation (MSDN) states : 1- config change, 2- Device added, 3-Device removed 4- Docking. For some reason I always get a value of 1.

任何想法?

这里是示例代码: p>

Here's sample code :

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;
    }
}


推荐答案

,我找不到代码。试过我的旧RAC帐号,没有。没有在我的旧备份。去搞清楚。但是我试图弄清楚我是如何做到的,我认为这是正确的顺序(我基于这个很大的一些文章):

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):


  1. 获取所有驱动器号并缓存

  2. 等待WM_DEVICECHANGE
    消息,并启动一个
    超时时间为1秒的定时器(这样做为
    避免了大量的虚假
    WM_DEVICECHANGE消息,一旦您插入
    USB钥匙/其他设备启动
    开始,只有在驱动器结算时才结束
    )。

  3. 将驱动器号与
    旧缓存进行比较,并检测新缓存。

  4. 获取设备信息。

  1. Get all drive letters and cache them.
  2. Wait for the WM_DEVICECHANGE 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").
  3. Compare the drive letters with the old cache and detect the new ones.
  4. Get device information for those.


    我知道还有其他方法,但这被证明是唯一一个可以在不同版本的Windows中一致工作的方法,我们需要这样做,因为我的客户端使用ActiveX控件从您插入的任何类型的设备上传图像的网页(我认为他们生产了某种印刷信息亭)。

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).

这篇关于WMI和Win32_DeviceChangeEvent - 返回错误的事件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆