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 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; }
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; }
6 Replies
and what are you hoping for?
its kinda hard to explain as im clueless in this but believe i'm supppose to get something like this wheres as im only receiving last to and one op ipv6 as fe: something if that anymore 😄
but what characteristics are you looking for in the IPs?
as i understand the user should be able to choose from a list of available ipv4 and ipv6 connections available on the computer like ethernet bluetooth local wifi and what not
sorry if that doesn't help that's all i really know 😄
so you want a list of local interfaces
which leads me to https://learn.microsoft.com/en-us/dotnet/api/system.net.networkinformation.networkinterface.getallnetworkinterfaces?view=net-8.0
Thanks a lot!