连接USB设备时如何收到通知?

我正在编写一个监控特定设备的程序。该设备可以或可以不总是连接,并且当连接时可以连接到几个不同端口中的任何一个;我希望我的程序可以优雅地处理这个问题。 有没有办法在连接特定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);
}
    
在SO上查看这个帖子。特别是使用SharpUSBLib的接受答案。     

要回复问题请先登录注册