windows form监听usb插拔
public partial class USBDeviceForm : Form
{
public const int WM_DEVICECHANGE = 0x219;
public const int DBT_DEVICEARRIVAL = 0x8000;
public const int DBT_CONFIGCHANGECANCELED = 0x0019;
public const int DBT_CONFIGCHANGED = 0x0018;
public const int DBT_CUSTOMEVENT = 0x8006;
public const int DBT_DEVICEQUERYREMOVE = 0x8001;
public const int DBT_DEVICEQUERYREMOVEFAILED = 0x8002;
public const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
public const int DBT_DEVICEREMOVEPENDING = 0x8003;
public const int DBT_DEVICETYPESPECIFIC = 0x8005;
public const int DBT_DEVNODES_CHANGED = 0x0007;
public const int DBT_QUERYCHANGECONFIG = 0x0017;
public const int DBT_USERDEFINED = 0xFFFF;
public USBDeviceForm ()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
try
{
if (m.Msg == WM_DEVICECHANGE)
{
switch (m.WParam.ToInt32())
{
case WM_DEVICECHANGE:
break;
case DBT_DEVICEARRIVAL:
MessageBox.Show("判断检测USB插入电脑");
DriveInfo[] s = DriveInfo.GetDrives();
foreach (DriveInfo drive in s)
{
if (drive.DriveType == DriveType.Removable)
{
break;
}
}
break;
case DBT_CONFIGCHANGECANCELED:
break;
case DBT_CONFIGCHANGED:
break;
case DBT_CUSTOMEVENT:
break;
case DBT_DEVICEQUERYREMOVE:
break;
case DBT_DEVICEQUERYREMOVEFAILED:
break;
case DBT_DEVICEREMOVECOMPLETE:
MessageBox.Show(""判断检测USB拔出电脑");
break;
case DBT_DEVICEREMOVEPENDING:
break;
case DBT_DEVICETYPESPECIFIC:
break;
case DBT_DEVNODES_CHANGED:
break;
case DBT_QUERYCHANGECONFIG:
break;
case DBT_USERDEFINED:
break;
default:
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
base.WndProc(ref m);
}
windows console 控制台 监听usb插拔
static Logger<Program> logger = new Logger<Program>();
static void Main(string[] args)
{
try
{
//查询所有设备的插拔事件
//Win32_DeviceChangeEvent Win32_VolumeChangeEvent
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2 or EventType = 3");
watcher.EventArrived += (s, e) =>
{
var txt = "";
foreach (var p in e.NewEvent.Properties)
{
txt += "name " + p.Name + " val " + p.Value + "\r\n";
}
Console.WriteLine(txt);
//string driveName = e.NewEvent.Properties["DriveName"].Value.ToString();
//EventType eventType = (EventType)(Convert.ToInt16(e.NewEvent.Properties["EventType"].Value));
//string eventName = Enum.GetName(typeof(EventType), eventType);
//Console.WriteLine("{0}: {1} {2}", DateTime.Now, driveName, eventName);
};
watcher.Query = query;
watcher.Start();
Console.Read();
//ServicesManager.Instance.StartServices();
//Thread.CurrentThread.IsBackground = false;
//Thread.Sleep(Timeout.Infinite);
}
catch (Exception e)
{
logger.Error("Main", e);
}
}
wpf 监听usb插拔
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
_intPtr = new WindowInteropHelper(this).Handle;
HwndSource hwndSource = HwndSource.FromHwnd(_intPtr);
// 添加处理程序
if (hwndSource != null) hwndSource.AddHook(HwndSourceHook);
}
public IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_DEVICECHANGE)
{
switch (wParam.ToInt32())
{
case DBT_DEVNODES_CHANGED: //已在系统中添加或删除设备。
Console.WriteLine("HwndSourceHook触发FindDevice");
DeviceManage.Instance.FDevice();//自定义处理方法
break;
//case DBT_DEVICEARRIVAL://已插入设备或介质,现已可用。
// MessageBox.Show("已插入设备或介质,现已可用。");
// break;
//case DBT_DEVICEREMOVECOMPLETE: //已删除设备或介质。
// MessageBox.Show("已删除设备或介质。");
// break;
default:
break;
}
}
return IntPtr.Zero;
}
}
龙腾一族至尊龙骑