【问题标题】:How might I receive notifications when a USB device is connected?连接 USB 设备时如何接收通知?
【发布时间】:2010-10-20 17:59:13
【问题描述】:

我正在编写一个程序来监控特定设备。此设备可能可能不始终处于连接状态,并且在连接时可以连接到多个不同端口中的任何一个;我希望我的程序能够优雅地处理这个问题。

有没有办法在连接特定 USB 设备时接收通知,并从那里确定它连接到哪个端口?

【问题讨论】:

  • 哪个操作系统?如果它的窗口有很多用于设备移除通知的 API。

标签: c# usb device


【解决方案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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    相关资源
    最近更新 更多