PC Review


Reply
Thread Tools Rate Thread

C# USB plug-in/removal: how to distingwish plug-in and removal ?

 
 
Polaris
Guest
Posts: n/a
 
      18th Mar 2008
Hi Experts:

I got some C# code from the web which detecting USB plug-in/removal using
System.Managment class (see below); but seems to me the information it
returus is the same for plug-in and removal. Is there a way to distingwish
these two actions using C#?

Thanks In Advance
Polaris

// This code demonstrates how to monitor the UsbControllerDevice 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; //set required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' ";
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.DiskEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
finally {
w.Stop();
}
}
public void DiskEventArrived(object sender, EventArrivedEventArgs e) {
//Get the Event object and display its properties (all)
foreach(PropertyData pd in e.NewEvent.Properties) {
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null) {
Console.WriteLine("--------------Properties------------------");
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}

}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non Removal of Altnet Browser Plug In Brian Anti-Spyware Installation 3 9th May 2005 06:19 PM
where and what is plug in manager and plug in interface? =?Utf-8?B?bWFoazQ3?= Windows XP Help 0 29th Apr 2004 11:16 AM
Java plug in removal =?Utf-8?B?b3Zlcm5pdGVyaWNr?= Microsoft Windows 2000 1 12th Jan 2004 05:19 AM
USB Stick Plug in and Plug out (USB Harddrive) Daniel Diehl Microsoft C# .NET 1 18th Dec 2003 09:41 PM
Detecting USB Stick Plug in and Plug out (USB Harddrive) Daniel Diehl Microsoft C# .NET 1 18th Dec 2003 01:02 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:33 PM.