C# USB Detection - winform and WPF
2010-12-23 14:41
429 查看
Win From Version:
代码
UsbDetector usbDetector;
public MainWindow()
{
InitializeComponent();
usbDetector = new UsbDetector();
usbDetector.StateChanged += new Lenovo.Common.Devices.UsbStateChangedEventHandler(usbDetector_StateChanged);
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper interop = new WindowInteropHelper(this);
HwndSource hwndSource = HwndSource.FromHwnd(interop.Handle);
HwndSourceHook hool = new HwndSourceHook(usbDetector.HwndHandler);
hwndSource.AddHook(hool); ;
usbDetector.RegisterDeviceNotification(interop.Handle);
}
void usbDetector_StateChanged(bool arrival)
{
if (arrival)
MessageBox.Show("add");
else
MessageBox.Show("removed");
}
In generally, the wParam should be DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE, but some time , it keep be the wParam =7 (DBT_DEVNODES_CHANGED) and LParam = 0, that is that the usb driver isn't correctly installed.
代码
UsbDetector usbDetector;
public MainWindow()
{
InitializeComponent();
usbDetector = new UsbDetector();
usbDetector.StateChanged += new Lenovo.Common.Devices.UsbStateChangedEventHandler(usbDetector_StateChanged);
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
WindowInteropHelper interop = new WindowInteropHelper(this);
HwndSource hwndSource = HwndSource.FromHwnd(interop.Handle);
HwndSourceHook hool = new HwndSourceHook(usbDetector.HwndHandler);
hwndSource.AddHook(hool); ;
usbDetector.RegisterDeviceNotification(interop.Handle);
}
void usbDetector_StateChanged(bool arrival)
{
if (arrival)
MessageBox.Show("add");
else
MessageBox.Show("removed");
}
In generally, the wParam should be DBT_DEVICEARRIVAL or DBT_DEVICEREMOVECOMPLETE, but some time , it keep be the wParam =7 (DBT_DEVNODES_CHANGED) and LParam = 0, that is that the usb driver isn't correctly installed.
相关文章推荐
- c# image GDI+发生一般性错误 & c# wpf use winform‘s Image
- WinForm(WPF) splash screen demo with C#
- 【WPF学习】WPF、WinForm(C#)多线程编程并更新界面(UI)/子线程更新主界面方式
- 在C# winform程序中调用WPF写的数学公式编辑器
- WPF、WinForm(C#)多线程编程并更新界面(UI)
- [Songqw.Net 基础]C#实现简单的插件化开发,可用于WPF Winform.
- WPF、WinForm(C#)多线程编程并更新界面(UI)
- WPF、WinForm(C#)多线程编程并更新界面(UI)
- C# 通用验证类 支持 WPF,MVC,Winform
- 如何获得当前应用程序的名字【Winform and WPF】
- 【原创】我的AVRUSB Receiver (C# And C)---2008.07
- C#用副线程改主线程(UI线程)的控件属性的方法(包括Winform和WPF)
- C#:USB设备枚举(七)WPF扩展方法
- WPF、WinForm(C#)多线程编程并更新界面(UI)(转载积累)
- WinForm C#: Simple Runtime Control ReSizing and Dragging & Moving Class
- C# WPF vs WinForm
- C# 在winform或者wpf中显示控制台窗口
- Lane Detection with OpenCV and C#
- c# Winform/WPF 开发分屏显示应用程序