Ethernet port insertion / removal detection in my winforms application?

I would like to determine when the device is connected to the Ethernet port of the computer on which my application is running. I know how to do this with a USB port, but the problem is that the port is not USB!

If it was a USB device, I would simply override WndProc and catch a message if it is WM_DEVICECHANGE, then I am the winner, I was wondering if it was as simple as this with any device that can be connected to the port?

I don’t want to know if something is happening or the device is working, just to find out if there was an insertion or deletion.

+6
c # winforms
source share
3 answers

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

Update

A brief study shows that NetworkChange.NetworkAddressChanged may 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(); } } 
+2
source share

I'm not sure if this is exactly right for your needs, but you can take 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 if the network interface has an Ethernet port

0
source share

The NetworkChange class provides you with the NetworkAvailabilityChanged event, which fires when the interfaces switch up or down. It may not be as low as you might be looking, but you are not definitely in your goal of tracking this event.

0
source share

All Articles