views:

195

answers:

3

Hi,

I would like to detect when a device is connected to an ethernet port of the machine that my application is running on. I know how to do this with a USB port but the problem is, the Port isn't USB!

If it was a USB device I would simply override WndProc and catch the message, if it is WM_DEVICECHANGE, then i'm on to a winner, I was wondering if it was as simple as this with any device that may be plugged into the port?

I don't want to know if there is anything happening, or if the device is working, just simply to find if there has been an insertion or removal.

Thanks in advance.

+2  A: 

I never used it myself, but I think that the NetworkChange.NetworkAvailabilityChanged event might fit your needs.

Update

A brief investigation indicates that NetworkChange.NetworkAddressChanged might work better:

public static void Main()
{

    NetworkChange.NetworkAddressChanged += (s, e) =>
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        foreach (var item in nics)
        {
            Console.WriteLine("Network Interface: {0}, Status: {1}", item.Name, item.OperationalStatus.ToString());
        }
    };

    string input = string.Empty;
    while (input != "quit")
    {
        input = Console.ReadLine();
    }
}
Fredrik Mörk
A: 

I'm not sure if that's exactly suited for your needs, but you could have a look at the System.Net.NetworkInformation.NetworkChange class, which has 2 events that you could use :

  • NetworkAddressChanged
  • NetworkAvailabilityChanged

In the event handler, you can check whether the network interface involved is an Ethernet port

Thomas Levesque
A: 

The NetworkChange class provides you with a NetworkAvailabilityChanged event triggered when interfaces switch from down to up or vice versa. Possibly not as low level as you might be looking for but you aren't specific in your objective in tracking this event.

Lazarus