问题
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