问题
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.
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?
回答1:
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);
}
来源:https://stackoverflow.com/questions/771205/how-might-i-receive-notifications-when-a-usb-device-is-connected