473,569 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c# code detect inset/remove events at usb port

hi
i wanna detect if a anything is connected to the usb port
I am using system.manageme nt class for tht purpose
this is my code

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
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 = "__InstanceOper ationEvent";
q.WithinInterva l = new TimeSpan(0,0,3) ;
q.Condition = @"TargetInstanc e ISA 'Win32_USBContr oller'";

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(); // block main thread for test purposes
}
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)
{
ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null)
{
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

but its not working.......? wats the error..it does not detect if
insert a device at the usb port..
wats the error..can anyone help me

Nov 17 '05 #1
6 24522
You are inserting/removing USB devices don't you?
The class you should watch is:
Win32_USBContro llerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi
i wanna detect if a anything is connected to the usb port
I am using system.manageme nt class for tht purpose
this is my code

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
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 = "__InstanceOper ationEvent";
q.WithinInterva l = new TimeSpan(0,0,3) ;
q.Condition = @"TargetInstanc e ISA 'Win32_USBContr oller'";

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(); // block main thread for test purposes
}
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)
{
ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null)
{
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

but its not working.......? wats the error..it does not detect if
insert a device at the usb port..
wats the error..can anyone help me

Nov 17 '05 #2
hi u r rite..
that work is done
thanx ..

event gets fired if anything is plugged in usb port
1)But the same event gets fired if device is inserted or removed..
so inside this event i wanna detect if device got inserted or
removed........ CAN ANYONE HELP ME??????
2)also i wanna find Total storage (capacity) and Available
storage(unused) of device........C AN ANYONE HELP ME??????

thanx



Willy Denoyette [MVP] wrote:
You are inserting/removing USB devices don't you?
The class you should watch is:
Win32_USBContro llerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi
i wanna detect if a anything is connected to the usb port
I am using system.manageme nt class for tht purpose
this is my code

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
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 = "__InstanceOper ationEvent";
q.WithinInterva l = new TimeSpan(0,0,3) ;
q.Condition = @"TargetInstanc e ISA 'Win32_USBContr oller'";

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(); // block main thread for test purposes
}
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)
{
ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null)
{
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

but its not working.......? wats the error..it does not detect if
insert a device at the usb port..
wats the error..can anyone help me


Nov 17 '05 #3

hi
ya u r rite..USBCONTRO LLERDEVICE..is to be used ........now the event
is getting fired..
the same event gets fired while inserting n removing device..
Inside this event i wanna write a code to display message "device
inserted " and when a device gets removed "device removed"..so inside
the evnet..i wanna checkif
1)the device is inserted or removed..HOw do i do tht??
2)also i wanna check Total storage (capacity)of the device connected
c) storage(unused) available in the device

how can i do this 3 things..can anyone help me????????????? ?

thanx

Willy Denoyette [MVP] wrote:
You are inserting/removing USB devices don't you?
The class you should watch is:
Win32_USBContro llerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. .
hi
i wanna detect if a anything is connected to the usb port
I am using system.manageme nt class for tht purpose
this is my code

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
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 = "__InstanceOper ationEvent";
q.WithinInterva l = new TimeSpan(0,0,3) ;
q.Condition = @"TargetInstanc e ISA 'Win32_USBContr oller'";

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(); // block main thread for test purposes
}
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)
{
ManagementBaseO bject mbo = null;
if(( mbo = pd.Value as ManagementBaseO bject) != null)
{
foreach(Propert yData prop in mbo.Properties)
Console.WriteLi ne("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

but its not working.......? wats the error..it does not detect if
insert a device at the usb port..
wats the error..can anyone help me


Nov 17 '05 #4
hai
is there any namespace should i declear for this code if s ur plz give the
namespace
Sep 12 '07 #5
Hi Sanjana,

Did you find the solution? My requirment is just same as yours, I wanna
detect if a device (for example barcode scanner) is connected to the usb
port. Can you give me the complete code please?

Shalabh

url:http://www.ureader.com/msg/14468359.aspx
Jan 2 '08 #6
hi , my problem is i want to attach a finger print scanner on any of the Usb
Port ?how could i detect the scanner as well as capture the image from it?

url:http://www.ureader.com/msg/14468359.aspx
Jan 8 '08 #7

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

Similar topics

1
13514
by: Mat | last post by:
How can I detect when a link has been clicked but the new page is still in the process of loading? The document.location.href property still displays the current location (understandably) not the one that's about to load. I have a page that reloads every 30 seconds in order to access live data. If a user clicks on a link just prior to the...
6
3987
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
15
4447
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc... there must be a way...
4
5975
by: Alex | last post by:
Is there any way to delete the value of a html hidden input field from code-behind? I use an html hidden input to store an array of data. I collect this data using Request.Form or Request.Params. After I use it, I want to force the deletion of it, so a user cannot hit their refresh button on their browser and trigger the same events that just...
13
31929
by: halbenca | last post by:
Is this possible with Javascript in anyway? :)
5
1815
by: Maciej Sondej | last post by:
Hi, I have problem. I want to react on event which come from IO.Port(RS232). I have a class that works on console application class Program { static void Main(string args) {
4
13263
by: e-Ricc | last post by:
Hi guys! Im trying to detect when a user "kills" my c# app using windows task manager, i've tried with the form events _closing and _closed managing for example putting an if inside to detect it: if (e.CloseReason == CloseReason.TaskManagerClosing) { MessageBox.Show("from task manager"); }
1
3686
by: Andy Bates | last post by:
Hi - Can't see another newsgroup to post this in; so thought I'd post here. I have a C# application that relies on multicast UDP to detect how many PCs the application is executing on concurrently. This works okay providing the port is open but fails miserably if the port is blocked by a firewall. All applications provide a server...
6
3815
by: patricktn | last post by:
Hi, In Windows programming, there is a built-in function to get MSSql database server names and connect to it without needing to know the port numbers. Is it possible to do this in Java (connect to MSSql database without knowing the port number used)? As far as I know, using JDBC, I would need to know the port numbers to connect to MSSql...
0
7694
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...
0
7609
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...
0
8118
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...
0
7964
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...
0
6278
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...
0
5217
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
0
936
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...

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.