Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Ethernet Port insertion/removal in my winforms application?

Tags:

c#

.net

winforms

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.

like image 814
Lloyd Powell Avatar asked Aug 12 '09 11:08

Lloyd Powell


1 Answers

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();
    }
}
like image 127
Fredrik Mörk Avatar answered Oct 03 '22 23:10

Fredrik Mörk