
Originally Posted by
4x2y
Following
jmcilhinney's suggestion, try this code:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetwork) ' Show IPV4
TextBox2.Text = GetHostIP(Net.Sockets.AddressFamily.InterNetworkV6)' Show IPV6
End Sub
Private Function GetHostIP(ByVal af As System.Net.Sockets.AddressFamily) As String
Dim host As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
Dim strRet As String = ""
For Each ip As System.Net.IPAddress In host.AddressList
If ip.AddressFamily = af Then
strRet = ip.ToString
Exit For
End If
Next
Return strRet
End Function
End Class
Not to say that there's anything wrong with that code, because the steps are all there laid out, but here's the concise version:
vb.net Code:
Dim address = Dns.GetHostEntry(Dns.GetHostName()).AddressList.FirstOrDefault(Function(ia) ia.AddressFamily = af)
Return If(address Is Nothing, CStr(Nothing), address.ToString())