By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,910 Members | 1,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,910 IT Pros & Developers. It's quick & easy.

c# code detect inset/remove events at usb port

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
hai
is there any namespace should i declear for this code if s ur plz give the
namespace
Sep 12 '07 #5

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.