如何在Windows XP机器上检测Windows服务中的传入USB设备? [英] How to detect an incoming USB device in windows Service on Windows XP machine?

查看:317
本文介绍了如何在Windows XP机器上检测Windows服务中的传入USB设备?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个简单的Windows服务,它将检测任何传入的USB设备并启动GUI应用程序。

  WqlEventQuery query = new WqlEventQuery(SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2); 
watcher.EventArrived + = watcher_EventArrived;
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();

此解决方案在Win 7中运行良好但是,在XP(我有XP SP3)中失败。



我在这里找到了另一个解决方案



http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-驱动



这在Windows窗体应用程序中甚至在Windows XP上的效果非常好,但是当我在Win 7上使用它时会失败。



您能帮忙吗?



编辑1:
目前没有解决方案



Bharat

解决方案

您可以简单地使用这两个解决方案,然后根据您所使用的Windows版本使用适当的解决方案。



使用 System.Environment.OSVersion 属性来告诉你所使用的Windows版本,然后执行CodeProject解决方案,如果它早于Windows 7,



您可以阅读更多关于由 System.Environment.OSVersion提供给您的信息属性此处


I an trying to write a simple Windows Service that shall detect any incoming USB device and Launch a GUI application.

            WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
            watcher.EventArrived += watcher_EventArrived;
            watcher.Query = query;
            watcher.Start();
            watcher.WaitForNextEvent();

This solution works well in Win 7 However, it fails in XP (I have XP SP3).

I found another solution here

http://www.codeproject.com/Articles/63878/Enumerate-and-Auto-Detect-USB-Drives

This works very well in windows forms application even on windows XP, However it fails when I put it in service even on Win 7

Can you please help?

EDIT 1: Presently there is no solution that works on win XP when we put the code in windows service.

Kind Regards

Bharat

解决方案

You could simply use both solutions, and then use the appropriate one depending on which version of Windows you are on.

Use the System.Environment.OSVersion property to tell you which version of windows you are on and then execute the CodeProject solution if it's earlier than Windows 7, and the other solution if it's Windows 7 or later.

You can read more about the information given back to you by the System.Environment.OSVersion property here.

这篇关于如何在Windows XP机器上检测Windows服务中的传入USB设备?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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