473,699 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10333

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.Manageme nt;
.....

//create a query to look for usb devices
WqlEventQuery w = new WqlEventQuery() ;
w.EventClassNam e = "__InstanceCrea tionEvent";
w.Condition = "TargetInst ance ISA 'Win32_USBContr ollerDevice'";
w.WithinInterva l = new TimeSpan(0,0,2) ;

//use a "watcher", to run the query
ManagementEvent Watcher watch = new ManagementEvent Watcher(w);
watch.EventArri ved+= new
EventArrivedEve ntHandler(this. usbDetectionHan dler);
watch.Start();

//when the watcher detects the query is true, it calls the registered
handler
//in this case usb.DetectionHa ndler
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*********@ya hoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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.Manageme nt;
....

//create a query to look for usb devices
WqlEventQuery w = new WqlEventQuery() ;
w.EventClassNam e = "__InstanceCrea tionEvent";
w.Condition = "TargetInst ance ISA 'Win32_USBContr ollerDevice'";
w.WithinInterva l = new TimeSpan(0,0,2) ;

//use a "watcher", to run the query
ManagementEvent Watcher watch = new ManagementEvent Watcher(w);
watch.EventArri ved+= new
EventArrivedEve ntHandler(this. usbDetectionHan dler);
watch.Start();

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

Revi

Dec 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
10276
by: Fazer | last post by:
Hello, I have a MP3 and I want to access the songs in them. I was wondering if Python could help me do that. I understand that this can be done using Linux by mountig it as a file system, but I am not on Linux and this is mainly for a Windows platform. Can anyone shed some light? Thanks,
0
1369
by: Hank | last post by:
Hi, I am writing a windows service application which manage image capturing device in C#.NET. When the service is running, how it automatically detect when there is a new device plugged in (say through a USB port or Ethernet Adapter)? Your help is highly appreciated! Hank
1
4585
by: Mark Smith | last post by:
Hello Group, I am using the RASAPI to dial-to the internet using a GPRS modem using C#. Is it possible before dialling to retreive information about the COM port that the modem is connected for the RAS entry in the phone book. What I mean is I have two entires, Entry A and Entry B using devices on COM 1 and COM 2 respectively.
0
1168
by: Mark Smith | last post by:
Hello Group, I am using the RASAPI to dial-to the internet using a GPRS modem using C#. Is it possible before dialling to retreive information about the COM port that the modem is connected for the RAS entry in the phone book. What I mean is I have two entires, Entry A and Entry B using devices on COM 1 and COM 2 respectively.
4
13738
by: Charles Law | last post by:
Is there an event or notification that my application can receive when a USB device (or other) is plugged in? I have looked at WMI as this appears to be the right area but cannot see how to get notification when this particular event occurs. The same goes for removal as well. TIA Charles
1
19357
by: hayworth | last post by:
Does anyone have any VB.Net code to get the devices listed / enumerated in the Device Manager listing. I'm trying to figure out if a device (it's actually a digital camera) is connected to the computer. When it's plugged in (Firewire), it shows up in Device Manager under the "Imaging Devices" catagory. When it's not plugged in, it doesn't show up. When it's plugged in, there is a registry key that says ReferenceCount = 1, and when...
2
4532
by: beaker | last post by:
Hi, I'm looking for a way of checking whether a specific USB device (a webcam) is plugged in. I have the Vendor and Product ID numbers for the device. I found an article (http://www.vsj.co.uk/articles/display.asp?id=600) with code sample that does the trick for human input devices (mouse, keyboard, ...) but am struggling for my webcam.
1
2504
by: rajeshrocks2006 | last post by:
Hi, Can anybody help me, how can i detect when any unauthorized device is plugged into the USB port? In my case an unauthorized USB device pen drive. I need to be able to detect if any pen drive is plugged into my machine and in a network..
0
8705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8943
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.