这个有难度,C#本身是肯定搞不定,我们可以使用C#封装一个事件,但是这个事件也要由消息队列中得到啊,现在就是不知道USB怎么样才能投递消息,这个可能会用到很多C++,c语言的东西,反正我是搞不定,找一个底层开发的地方去问问!哈
PS:这里有点相似的资料,参考一下,我也研究一下
http://www.cnblogs.com/virusswb/archive/2008/08/22/1274085.html
http://topic.csdn.net/t/20050722/17/4162216.html
这样应该可以,都是利用WM_DEVICECHANGE消息的,里面可能有些你需要的参数
调api
学习....
用System.Managment class 试试
下面是我在网上找到的代码
出处
http://www.eggheadcafe.com/software/aspnet/31850441/c-usb-pluginremoval-h.aspx
代码如下:
// 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);
}
}
}
}
下面几篇文章也提供给你参考
MSDN 中介绍如何注册驱动的通知消息
http://msdn.microsoft.com/en-us/library/aa363432.aspx
Detecting USB Stick Plug in and Plug out (USB Harddrive)
http://bytes.com/groups/net-c/234006-detecting-usb-stick-plug-plug-out-usb-harddrive