J
Jeff
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
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