检测USB设备事件



我制作了一个控制台应用程序,该应用程序可检测所有类型的USB设备的插件和插件事件。但是我想要一些过滤器,就像我只想检测网络摄像头一样。这是通过使用GUID类完成的。网络摄像头的类是带有GUID的"图像"类," {6BDD1FC5-810F-11D0-BEC7-08002BE2092F}"。问题是此'image'类也用于扫描仪,我不想检测扫描仪。代码为扫描仪。下面给出:

static void Main(string[] args)
    {
        WqlEventQuery weqQuery = new WqlEventQuery();
        weqQuery.EventClassName = "__InstanceOperationEvent";
        weqQuery.WithinInterval = new TimeSpan(0, 0, 3);
        weqQuery.Condition = @"TargetInstance ISA 'Win32_PnPEntity'";
        ManagementEventWatcher m_mewWatcher = new ManagementEventWatcher(weqQuery);
        m_mewWatcher.EventArrived += new EventArrivedEventHandler(m_mewWatcher_EventArrived);
        m_mewWatcher.Start();
        Console.ReadLine();
    }
    static void m_mewWatcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        bool bUSBEvent = false;
        string deviceCaption = "";
        string deviceType = "";
        foreach (PropertyData pdData in e.NewEvent.Properties)
        {
            try
            {
                ManagementBaseObject mbo = (ManagementBaseObject)pdData.Value;
                if (mbo != null)
                {
                    foreach (PropertyData pdDataSub in mbo.Properties)
                    {
                        Console.WriteLine(pdDataSub.Name + " = " + pdDataSub.Value);
                        if (pdDataSub.Name == "Caption")
                        {
                            deviceCaption = pdDataSub.Value.ToString();
                        }
                        if (pdDataSub.Name == "ClassGuid" && pdDataSub.Value.ToString() == "{6bdd1fc5-810f-11d0-bec7-08002be2092f}")
                        {
                            bUSBEvent = true;
                            deviceType = "Image";
                        }
                    }
                    if (bUSBEvent)
                    {
                        if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
                        {
                            Console.WriteLine("A " + deviceType + " device " + deviceCaption + " was plugged in at " + DateTime.Now.ToString());
                        }
                        else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
                        {
                            Console.WriteLine("A " + deviceType + " device " + deviceCaption + " was plugged out at " + DateTime.Now.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

有关参考检查此链接

我等了,但是没有任何身体回答这个问题,因此,在看到 ManagementBaseObject的所有属性之后,我发现有一个名为服务的属性扫描仪有所不同。在扫描仪中,服务属性的价值为 USBSCAN ,而在相机中是USBVIDEO。例如。你可以做这样的事情

if (mbo.Properties["Service"].Value.ToString() == "usbscan")
{
//then it means it is a scanner
}
else
{
//then it means it is a camera
}

注意:主要问题是我们如何区分扫描仪和网络摄像头,因为它们都使用相同的GUID。

相关内容

  • 没有找到相关文章

最新更新