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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With