How do I get only Internet Protocol version 4 addresses from Dns.GetHostAddresses()? I have the code below, and it gives me IPv4 and IPv6 addresses.
I have to m
For me the cleaner solution would be:
public static string GetLocalIP()
{
string ipv4Address = String.Empty;
foreach (IPAddress currentIPAddress in Dns.GetHostAddresses(Dns.GetHostName()))
{
if (currentIPAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipv4Address = currentIPAddress.ToString();
break;
}
}
return ipv4Address;
}