472,801 Members | 1,280 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.management class for tht purpose
this is my code

class usbdetect
{
public static void Main()
{
usbdetect we = new usbdetect();
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 = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBController'";

Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
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)
{
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null)
{
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{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 24390
You are inserting/removing USB devices don't you?
The class you should watch is:
Win32_USBControllerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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();
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 = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBController'";

Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
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)
{
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null)
{
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{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........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_USBControllerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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();
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 = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBController'";

Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
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)
{
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null)
{
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{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..USBCONTROLLERDEVICE..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_USBControllerDevice
not the controller itself...
Willy.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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();
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 = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_USBController'";

Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.UsbEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
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)
{
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null)
{
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{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
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...
6
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
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...
4
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....
13
by: halbenca | last post by:
Is this possible with Javascript in anyway? :)
5
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
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...
1
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...
6
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.