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.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);
}
}
}
}
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
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.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);
}
}
}
}
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