473,656 Members | 2,793 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 34715
Jack, you are going to have to use P/Invoke and RegisterDeviceN otification.
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******** ******@TK2MSFTN GP11.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_SPA M_gewing.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Jack, you are going to have to use P/Invoke and RegisterDeviceN otification. 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******** ******@TK2MSFTN GP11.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_SPA M_gewing.com> wrote in message
news:#1******** ******@TK2MSFTN GP12.phx.gbl...
Jack, you are going to have to use P/Invoke and RegisterDeviceN otification. 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******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP11.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.Manageme nt classes (and WMI).

// This code demonstrates how to monitor the UsbControllerDe vice for
// the arrival of creation/operation events
using System;
using System.Componen tModel;
using System.Runtime. InteropServices ;
using System.Manageme nt;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEvent Watcher w= null;
WqlEventQuery q;
ManagementOpera tionObserver observer = new ManagementOpera tionObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope ("root\\CIMV2") ;
scope.Options.E nablePrivileges = true; //sets required privilege
try {
q = new WqlEventQuery() ;
q.EventClassNam e = "__InstanceCrea tionEvent";
q.WithinInterva l = new TimeSpan(0,0,10 );

q.Condition = @"TargetInstanc e ISA 'Win32_USBContr ollerDevice' ";
Console.WriteLi ne(q.QueryStrin g);
w = new ManagementEvent Watcher(scope, q);

w.EventArrived += new EventArrivedEve ntHandler(we.Us bEventArrived);
w.Start();
Console.ReadLin e();
}
catch(Exception e) {
Console.WriteLi ne(e.Message);
}
finally {
w.Stop();
}
}
public void UsbEventArrived (object sender, EventArrivedEve ntArgs e) {
//Get the Event object and display it
foreach(Propert yData pd in e.NewEvent.Prop erties) {
Console.WriteLi ne("\n========= =============== ==============" );
Console.WriteLi ne("{0},{1},{2} , {3}",pd.Name, pd.Type, pd.Value, pd.Origin);

ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null) {
Console.WriteLi ne("--------------Properties------------------");
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{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
1307
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 re-directs to an interface specific for that set of devices. Else if a device such as a desktop or notebook is being used it will re-direct the user to a different interface.
0
1219
by: Viper99 | last post by:
I need to detect or be notified of PCMCIA device removal using C# .Net.
1
2929
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 into a PC (and is identified as a mass-storage device). However i cant fugure out how to do it using c#. regards john
6
24533
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 = new usbdetect();
5
9525
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 wired as well. Any Help would be appreciated. Regards Al
0
1233
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 the server which run these aspx pages. now my question is 1- how to detect this mobile phone which connected through serial ports 2- how I could send SMS through this mobile to other mobile devices, if anyone has any suggestion please help me
4
7005
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 the server which run these aspx pages. now my question is 1- how to detect this mobile phone which connected through serial ports 2- how I could send SMS through this mobile to other mobile devices, if anyone has any suggestion please help me
1
2409
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
1739
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 the clock signal from the "device" I want to control. Are there any examples of Pyserial being used this way? Thanks for any help.
2
2608
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 and changing the settings but it has not helped. Any suggestions?
0
8382
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
8816
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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
8498
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
7311
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...
0
5629
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1600
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.