471,319 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

determine if a specific USB device is plugged in

I want to check if a USB device is availble on a system. Is this possible?
I see it in the Device Manager, so I think it must be possible "somehow" :)
Dec 16 '05 #1
3 10072

Steve wrote:
I want to check if a USB device is availble on a system. Is this possible?
I see it in the Device Manager, so I think it must be possible "somehow" :)


Well, yes, its possible.

Check out: http://www.codeproject.com/csharp/DivingSysProg2.asp

The class name is simple enough, the problem will be figuring out which
one is
what you want.

Matt

Dec 16 '05 #2
You don't need to go to all those unmanaged API's. The .NET WMI
wrappers provides clean access to system devices.

Here is a code snippet that will let you determine if a usb device is
plugged in. Your code will change slightly depending on what type (i.e.
CLASS) the usb device is. In any case to be alerted when a usb device
is plugged in, you can use a WQL (Windows Query Language) query, and
register a handler, as follows:

using System.Management;
.....

//create a query to look for usb devices
WqlEventQuery w = new WqlEventQuery();
w.EventClassName = "__InstanceCreationEvent";
w.Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
w.WithinInterval = new TimeSpan(0,0,2);

//use a "watcher", to run the query
ManagementEventWatcher watch = new ManagementEventWatcher(w);
watch.EventArrived+= new
EventArrivedEventHandler(this.usbDetectionHandler) ;
watch.Start();

//when the watcher detects the query is true, it calls the registered
handler
//in this case usb.DetectionHandler
Hope this helps. If you have follow up queries do let me know.

Revi

Dec 17 '05 #3
Great, this is very cool!
Currently I'm getting a list of all devices and doing a string search to
determine if the one that I'm interested in is present, then I check it's
state and error code. But I only do this at startup and was wondering how I
would handle someone REMOVING the device... now I know, thank you!

-Steve
<re*********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
You don't need to go to all those unmanaged API's. The .NET WMI
wrappers provides clean access to system devices.

Here is a code snippet that will let you determine if a usb device is
plugged in. Your code will change slightly depending on what type (i.e.
CLASS) the usb device is. In any case to be alerted when a usb device
is plugged in, you can use a WQL (Windows Query Language) query, and
register a handler, as follows:

using System.Management;
....

//create a query to look for usb devices
WqlEventQuery w = new WqlEventQuery();
w.EventClassName = "__InstanceCreationEvent";
w.Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
w.WithinInterval = new TimeSpan(0,0,2);

//use a "watcher", to run the query
ManagementEventWatcher watch = new ManagementEventWatcher(w);
watch.EventArrived+= new
EventArrivedEventHandler(this.usbDetectionHandler) ;
watch.Start();

//when the watcher detects the query is true, it calls the registered
handler
//in this case usb.DetectionHandler
Hope this helps. If you have follow up queries do let me know.

Revi

Dec 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Fazer | last post: by
1 post views Thread by Mark Smith | last post: by
reply views Thread by Mark Smith | last post: by
1 post views Thread by hayworth | last post: by
2 posts views Thread by beaker | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.