usb port insert/remove event detection

S

sanjana

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_USBControllerdevice'";


Console.WriteLine(q.QueryStrin­g);
w = new ManagementEventWatcher(scope,
q);


w.EventArrived += new
EventArrivedEventHandler(we.Us­bEventArrived);
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);
}
}
}


}




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
 
W

Willy Denoyette [MVP]

I posted this code snip to get you started, now it's up to you to try
understand what the code is doing and how it can be adapted to suit your
needs. Start reading the WMI docs in MSDN, search for the event types you
can watch for, like __InstanceOperationEvent, __InstanceCreationEvent and
__InstanceDeletionEvent and try to understand what they are and what they
might be used for.

Just a hint, the InstanceCreationEvent is fired when a new instance of
'Win32_USBControllerdevice' is recorded in the metabase, so this is the
event to watch for when you need USB insert detection.....

Willy.

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_USBControllerdevice'";


Console.WriteLine(q.QueryStrin­g);
w = new ManagementEventWatcher(scope,
q);


w.EventArrived += new
EventArrivedEventHandler(we.Us­bEventArrived);
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);
}
}
}


}




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
 
S

sanjana

Hi
thanx
i got it
but i m making some minute error which i m not able to figure out..
i am able to detect insert/remove events..but the evnts are getting
fired twice
tht is if i remove a device at the usb port..the deviceremoveevent
gets executed twice..n if insert a device..the insert event get s
exeuted twice..
am i making some error......


thanx
 
S

sanjana

Hi
thanx
i got it
but i m making some minute error which i m not able to figure out..
i am able to detect insert/remove events..but the evnts are getting
fired twice
tht is if i remove a device at the usb port..the deviceremoveevent
gets executed twice..n if insert a device..the insert event get s
exeuted twice..
am i making some error......??


thanx
 
S

sanjana

Hi
thanx
i got it
but i m making some minute error which i m not able to figure out..
i am able to detect insert/remove events..but the evnts are getting
fired twice
tht is if i remove a device at the usb port..the deviceremoveevent
gets executed twice..n if insert a device..the insert event get s
exeuted twice..
am i making some error......??


thanx
 
S

sanjana

ooooops!! i got y the evnt gets fired twice..actually its getting
executed twice cause of 2 devices present :)
 
W

Willy Denoyette [MVP]

Depending on the device it's quite possible that even more events gets
fired.
USB devices are quite complex things, and mostly they consists of more than
one logical device. The device driver mostly consists of several layers and
each layer is able to signal it presence by creating a WMI class instance
when inserted.
What you should do is filter the events according the properties of
interest, but again this depends highly on the device.

Willy.


ooooops!! i got y the evnt gets fired twice..actually its getting
executed twice cause of 2 devices present :)
 
S

sanjana

hi willy
ok thanx for advice
ok is there way to detect if
sd card is insered or removed from the device at usb
tht is i want to check sd card insert/remove events

thanx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top