我正在编写一个程序来监视特定设备。该设备可能会或可能不会始终连接,并且在连接时可以连接到多个不同端口中的任何一个;我希望我的程序能够优雅地处理这个问题。
有没有办法在连接特定 USB 设备时接收通知,并从那里确定它连接到哪个端口?
要获取任何硬件设备已更改的信息,您可以将以下代码添加到主窗体:
/// <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);
}