How to detect an incoming USB device in windows Service on Windows XP machine?

↘锁芯ラ 提交于 2019-12-24 09:42:50

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15810777/how-to-detect-an-incoming-usb-device-in-windows-service-on-windows-xp-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!