C#C
C#3y ago
MisterT

Need help on getting a list of IPv4 and IPv6 IP Addresses

This is what I have at the moment don't even now what it does, but ain't doing what I'm hoping forpepecry public static List<string> GetLocalIPAddresses()
{
List<string> ipAddresses = new List<string>();

string hostName = Dns.GetHostName();

IPHostEntry hostEntry = Dns.GetHostEntry(hostName);

foreach (IPAddress ip in hostEntry.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddresses.Add(ip.ToString());
}
else if (ip.AddressFamily == AddressFamily.InterNetworkV6 && !ip.IsIPv6LinkLocal)
{
ipAddresses.Add(ip.ToString().Split('%').FirstOrDefault());
}
}

if (!ipAddresses.Contains("127.0.0.1"))
{
ipAddresses.Insert(0, "127.0.0.1");
}

return ipAddresses;
}
Was this page helpful?