Using the System.Net.NetworkInformation
namespace I came to the following solution:
public static NetworkInterface GetInterfaceForIP(IPAddress address)
{
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (address.Equals(ip.Address))
return ni;
}
}
}
return null;
}
Without much error checking here, this function returns the network interface for the IP.
If you need to check for status change events (cable unplugged, etc) can use the NetworkChange
event in the System.Net.NetworkInformation
namespace.