Hi, I would like to get the public IP of the computer running my app without looking it up on a website.
I found this online, but even after applying all the IPv6 filters at least one remains.
I figured I could just filter the addresses with a simple regex and this does work perfectly.
But I am just curious if there is a better way of getting the IP address that does not require me to use other deprecated methods or loop/filter arrays in order to get at the address.
I found this online, but even after applying all the IPv6 filters at least one remains.
VB.NET Code:
Dim IPV4AddressesV2 As IEnumerable(Of IPAddress) = _ Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where( _ Function(a As IPAddress) _ Not a.IsIPv6LinkLocal _ AndAlso Not a.IsIPv6Multicast _ AndAlso Not a.IsIPv6SiteLocal _ AndAlso Not a.IsIPv6Teredo _ AndAlso Not a.IsIPv4MappedToIPv6) MessageBox.Show(String.Join(Environment.NewLine, IPV4AddressesV2))
I figured I could just filter the addresses with a simple regex and this does work perfectly.
VB.NET Code:
Dim IPV4AddressesV1 As IEnumerable(Of IPAddress) = _ Dns.GetHostEntry(Dns.GetHostName).AddressList.Where( _ Function(IP As IPAddress) Regex.IsMatch(IP.ToString, "^(\d{1,3}\.){3}\d{1,3}$")) MessageBox.Show(String.Join(Environment.NewLine, IPV4AddressesV1))