473,503 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 34702
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
1297
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
1201
by: Viper99 | last post by:
I need to detect or be notified of PCMCIA device removal using C# .Net.
1
2903
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
24504
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
9503
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
1199
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
6995
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
2395
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
1730
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
2595
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
7202
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,...
0
7086
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...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
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...
0
5578
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,...
1
5014
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.