473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to detect a USB Device Being plugged-in or unplugged?

Hello,

I'm trying to make a program what can let me know when a USB device is added
or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack
Nov 20 '05 #1
4 18677
Jack, you are going to have to use P/Invoke and RegisterDeviceNotification.
Here's a link to some code by Wei Mao at MS.

http://www.dotnet247.com/247referenc...32/164968.aspx

--
Greg Ewing [MVP]
http://www.citidc.com

"jack" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm trying to make a program what can let me know when a USB device is added or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack

Nov 20 '05 #2
Hello,

Great, any example in VB.NET?

Thanks,

Jack
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Jack, you are going to have to use P/Invoke and RegisterDeviceNotification. Here's a link to some code by Wei Mao at MS.

http://www.dotnet247.com/247referenc...32/164968.aspx

--
Greg Ewing [MVP]
http://www.citidc.com

"jack" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm trying to make a program what can let me know when a USB device is

added
or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack


Nov 20 '05 #3
YOU ARE THE MAN!
"Greg Ewing [MVP]" <gewing@_NO_SPAM_gewing.com> wrote in message
news:#1**************@TK2MSFTNGP12.phx.gbl...
Jack, you are going to have to use P/Invoke and RegisterDeviceNotification. Here's a link to some code by Wei Mao at MS.

http://www.dotnet247.com/247referenc...32/164968.aspx

--
Greg Ewing [MVP]
http://www.citidc.com

"jack" <ja**@mrolinux.com> wrote in message
news:eM**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm trying to make a program what can let me know when a USB device is

added
or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack


Nov 20 '05 #4

"jack" <ja**@mrolinux.com> wrote in message news:eM**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm trying to make a program what can let me know when a USB device is added
or removed from a system. If anyone has any ideas or help that would be
great!

Thanks in advnace,

Jack


Here is a sample using the System.Management classes (and WMI).

// This code demonstrates how to monitor the UsbControllerDevice for
// the arrival of creation/operation events
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //sets required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceCreationEvent";
q.WithinInterval = new TimeSpan(0,0,10);

q.Condition = @"TargetInstance ISA 'Win32_USBControllerDevice' ";
Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine();
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
finally {
w.Stop();
}
}
public void UsbEventArrived(object sender, EventArrivedEventArgs e) {
//Get the Event object and display it
foreach(PropertyData pd in e.NewEvent.Properties) {
Console.WriteLine("\n============================= =========");
Console.WriteLine("{0},{1},{2}, {3}",pd.Name, pd.Type, pd.Value, pd.Origin);

ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null) {
Console.WriteLine("--------------Properties------------------");
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

Willy.

Nov 20 '05 #5

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

Similar topics

0
by: Andy Warr | last post by:
Hello... I am developing an application using C# and ASP.NET. I wish my code to detect the device the user is using (OS, Browser) and if a mobile device is being used (PDA, Phone, etc..) it...
0
by: Viper99 | last post by:
I need to detect or be notified of PCMCIA device removal using C# .Net.
1
by: John Rauhe | last post by:
Hello, Does anybody know how to detect if an mass-storage device has been added to the system ? I am making a program that will (should) detect when a CompactFlash memory card has been inserted...
6
by: sanjana | last post by:
hi i wanna detect if a anything is connected to the usb port I am using system.management class for tht purpose this is my code class usbdetect { public static void Main() { usbdetect we =...
5
by: Al | last post by:
Hi, Is there any way to detected if a device is connected? How would I query to see the device is connected to network. My application is mobile and mostly on wireless connection but it could be...
0
by: Sara | last post by:
Hi I want to code a program which could send SMS through web page to mobile phone, but because there is no Service here such as SMPP , therefore I have GSM mobile phone which connected directly to...
4
by: Sara | last post by:
Hi I want to code a program which could send SMS through web page to mobile phone, but because there is no Service here such as SMPP , therefore I have GSM mobile phone which connected directly to...
1
by: vishal | last post by:
I have a removable device attached to the pc. I want some code to detect that particular device. Can I achieve this thru vb.net? If yes just help me regarding this.. Regards vishal.
0
by: TonyAm | last post by:
I've just gotten started with Python and would like to control a device that accepts synchronous serial input. Rather than just having serial data streaming out via pyserial, I need to also detect...
2
by: KatherineK | last post by:
I have a Dell Inspiron 1520. Last week my screen started randomly dimming when I plugged it into the charger. When I unplug it, it goes bright again. I have tried going into the control panel...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.