C# USB拔插监听
C#查找设备管理器中所有的 USB 设备
wpf、winform 监听USB拔插时触发
监听Windows USB 拔插时触发
csharp
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
FleckWebSocketConfig.OpenSocketConfig().GetAwaiter(); //websocket 服务开启
用于监听Windows消息
注意获取窗口句柄一定要写在窗口loaded事件里,才能获取到窗口句柄,否则为空
hwndSource = PresentationSource.FromVisual(this) as HwndSource;//窗口过程
if (hwndSource != null)
hwndSource.AddHook(new HwndSourceHook(_godeComeraBuilder.DeveiceChanged)); //挂钩 _godeComeraBuilder.DeveiceChanged 要挂在的方法,USB拔插时触发改方法
}
csharp
public IntPtr DeveiceChanged(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){
return IntPtr.Zero;
}