The code below will find the local IP address and not the public IP if you are behind a router.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
TextBox1.Text = GetIpAddressLinq.ToString()
'TextBox1.Text = GetIpAddress.ToString()
End Sub
'Use linq
Public Function GetIpAddressLinq() As Net.IPAddress
Dim host = Net.Dns.GetHostEntry(Net.Dns.GetHostName())
Return host.AddressList.FirstOrDefault(Function(x As Net.IPAddress) _
x.AddressFamily = Net.Sockets.AddressFamily.InterNetwork)
End Function
'Use for next loop
Public Function GetIpAddress() As Net.IPAddress
Dim host = Net.Dns.GetHostEntry(Net.Dns.GetHostName())
For Each address In host.AddressList
If address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
Return address
End If
Next
Return Nothing
End Function
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.