Get current work ip address

I am looking for a solution that allows me to get the currently working / (in use) ip address of my computer / laptop.

I mean, I can be on a local network and then switch to WLAN. The ip addy will change and I need to keep abreast of the latest developments.

This is why I am not happy with the approaches I have found on the Internet, such as IPAddress [] localIPs = Dns.GetHostAddresses (Dns.GetHostName ());

What's the best approach to do this?

+3


source to share


1 answer


You can find the active interfaces of the machine as follows:

var interfaces = NetworkInterface.GetAllNetworkInterfaces().Where(a => a.OperationalStatus == OperationalStatus.Up);

var addresses = interfaces.First().GetIPProperties().UnicastAddresses.Where(a => a.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);

      



The property of Address

any element in addresses

contains ip. From there, you can control the adapters and decide when you need to switch and which address to bind.

+2


source







All Articles