连接USB设备后如何接收通知?

RV.*_*RV. 5 c# usb device

我正在编写一个程序来监视特定的设备。此设备可能会可能不会一直保持连接,并且在连接时可能会连接到几个不同端口中的任何一个;我希望我的程序能够优雅地处理这个问题。

当连接了特定的USB设备时,是否有方法可以接收通知,并从那里确定连接到哪个端口?

Oli*_*ver 1

要获取任何硬件设备是否发生更改的信息,您可以将以下代码添加到主窗体中:

/// <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);
}
Run Code Online (Sandbox Code Playgroud)