473,320 Members | 1,865 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
Jul 21 '05 #1
4 34689
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

Jul 21 '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


Jul 21 '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


Jul 21 '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.

Jul 21 '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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.