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

usb port insert/remove event detection

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_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

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


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

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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
Nov 17 '05 #2

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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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


Nov 17 '05 #3

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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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


Nov 17 '05 #4

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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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


Nov 17 '05 #5

P: n/a
ooooops!! i got y the evnt gets fired twice..actually its getting
executed twice cause of 2 devices present :)
sanjana wrote:
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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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


Nov 17 '05 #6

P: n/a
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.
"sanjana" <su****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
ooooops!! i got y the evnt gets fired twice..actually its getting
executed twice cause of 2 devices present :)
sanjana wrote:
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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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

Nov 17 '05 #7

P: n/a
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
Willy Denoyette [MVP] wrote:
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.
"sanjana" <su****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
ooooops!! i got y the evnt gets fired twice..actually its getting
executed twice cause of 2 devices present :)
sanjana wrote:
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

Willy Denoyette [MVP] wrote:
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.

"sanjana" <su****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.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_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


Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.