Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27474

VS 2012 Best way to get public IP in IPV4 format

$
0
0
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.
VB.NET Code:
  1. Dim IPV4AddressesV2 As IEnumerable(Of IPAddress) = _
  2. Dns.GetHostEntry(Dns.GetHostName()).AddressList.Where( _
  3.     Function(a As IPAddress) _
  4.         Not a.IsIPv6LinkLocal _
  5.         AndAlso Not a.IsIPv6Multicast _
  6.         AndAlso Not a.IsIPv6SiteLocal _
  7.         AndAlso Not a.IsIPv6Teredo _
  8.         AndAlso Not a.IsIPv4MappedToIPv6)
  9.  
  10. 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:
  1. Dim IPV4AddressesV1 As IEnumerable(Of IPAddress) = _
  2. Dns.GetHostEntry(Dns.GetHostName).AddressList.Where( _
  3.     Function(IP As IPAddress) Regex.IsMatch(IP.ToString, "^(\d{1,3}\.){3}\d{1,3}$"))
  4.  
  5. MessageBox.Show(String.Join(Environment.NewLine, IPV4AddressesV1))
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.

Viewing all articles
Browse latest Browse all 27474

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>