连接USB设备后如何接收通知? [英] How might I receive notifications when a USB device is connected?
本文介绍了连接USB设备后如何接收通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序来监视特定的设备。此设备可能或可能不始终处于连接状态,并且在连接时可以连接到多个不同端口中的任何一个。我希望我的程序能够妥善处理此问题。
I'm writing a program to monitor a specific device. This device may or may not always be connected, and when connected may be connected to any one of several different ports; I'd like my program to handle this gracefully.
是否有一种方法可以在连接特定的USB设备时接收通知,并从那里确定它是哪个端口
Is there a way to receive notifications when a specific USB device is connected, and from there to determine which port it is connected to?
推荐答案
要获取有关任何硬件设备已更改的信息,可以在主表单中添加以下代码:
To get an information if any hardware device has changed you can add the following code to your main form:
/// <summary>
/// Windows Messages
/// Defined in winuser.h from Windows SDK v6.1
/// Documentation pulled from MSDN.
/// For more look at: http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html
/// </summary>
public enum WM : uint
{
/// <summary>
/// Notifies an application of a change to the hardware configuration of a device or the computer.
/// </summary>
DEVICECHANGE = 0x0219,
}
protected override void WndProc(ref Message m)
{
switch ((WM)m.Msg)
{
case WM.DEVICECHANGE:
//ToDo: put your code here.
break;
}
base.WndProc(ref m);
}
这篇关于连接USB设备后如何接收通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文