Need to detect insertion and removal of USB Drive

J

Jeff

Guest
I have located the following code and trying to change this so I know if
the USB device has been inserted or removed.

The problem I have is I alwayd get the Console.WriteLine("Usb removed")
so I hope someone can point me to where I made my mistake .

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) {
if ((mbo.ClassPath.ClassName == "__InstanceCreationEvent"))
{
Console.WriteLine("Usb inserted");

} else {
Console.WriteLine("Usb removed");

}
Console.WriteLine( (string)mbo["Name"]);

foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
Console.WriteLine("-----------------------------------");


Full Code below

/*
* Created by SharpDevelop.
* User: Jeff
* Date: 23/11/2007
* Time: 11:11 AM
*
* To change this template use Tools | Options | Coding | Edit Standard
Headers.
*/
// This code demonstrates how to monitor the 'Win32_DiskDrive' for
// the arrival of creation/operation events
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
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_DiskDrive' ";

//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) {
if ((mbo.ClassPath.ClassName == "__InstanceCreationEvent"))
{
Console.WriteLine("Usb inserted");

} else {
Console.WriteLine("Usb removed");

}
Console.WriteLine( (string)mbo["Name"]);

foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
Console.WriteLine("-----------------------------------");
}
}
}

}


Regards
Jeff
 


On 23 nov, 08:19, Jeff <jeff@[_nospam_].hardsoft.com.au> wrote:
> I have located the following code and trying to change this so I know if
> the USB device has been inserted or removed.
>
> The problem I have is I alwayd get the Console.WriteLine("Usb removed")
> so I hope someone can point me to where I made my mistake .
>
> 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) {
> if ((mbo.ClassPath.ClassName == "__InstanceCreationEvent"))
> {
> Console.WriteLine("Usb inserted");
>
> } else {
> Console.WriteLine("Usb removed");
>
> }
> Console.WriteLine( (string)mbo["Name"]);
>
> foreach(PropertyData prop in mbo.Properties)
> Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
> Console.WriteLine("-----------------------------------");
>
> Full Code below
>
> /*
> * Created by SharpDevelop.
> * User: Jeff
> * Date: 23/11/2007
> * Time: 11:11 AM
> *
> * To change this template use Tools | Options | Coding | Edit Standard
> Headers.
> */
> // This code demonstrates how to monitor the 'Win32_DiskDrive' for
> // the arrival of creation/operation events
> using System;
> using System.ComponentModel;
> using System.Runtime.InteropServices;
> using System.Management;
> class WMIEvent {
> public static void Main() {
> WMIEvent we = new WMIEvent();
> 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_DiskDrive' ";
>
> //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) {
> if ((mbo.ClassPath.ClassName == "__InstanceCreationEvent"))
> {
> Console.WriteLine("Usb inserted");
>
> } else {
> Console.WriteLine("Usb removed");
>
> }
> Console.WriteLine( (string)mbo["Name"]);
>
> foreach(PropertyData prop in mbo.Properties)
> Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
> Console.WriteLine("-----------------------------------");
>
> }
> }
> }
> }
>
> Regards
> Jeff


Jeff,

I found a solution, mbo has to be fixed with e.NewEvent and not with a
Properties :




/*
* Created by SharpDevelop.
* User: Jeff
* Date: 23/11/2007
* Time: 11:11 AM
*
* To change this template use Tools | Options | Coding | Edit
Standard
Headers.
*/
// This code demonstrates how to monitor the 'Win32_DiskDrive' for
// the arrival of creation/operation events
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent
{
public static void Main()
{
WMIEvent we = new WMIEvent();
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_DiskDrive' ";


//Console.WriteLine(q.QueryString);
w = new ManagementEventWatcher(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)
{


ManagementBaseObject mbo = null;

mbo = (ManagementBaseObject)e.NewEvent;
if ((mbo.ClassPath.ClassName == "__InstanceCreationEvent"))
{
Console.WriteLine("Usb inserted");


}
else
{
Console.WriteLine("Usb removed");


}
Console.WriteLine((string)mbo["Name"]);


foreach (PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name,
prop.Value);

Console.WriteLine("-----------------------------------");





Console.WriteLine("******************************");
}
}


 

Back
Top