Show users ip on textbox1

Conejo

Well-known member
Joined
Jul 24, 2013
Messages
65
Location
USA
Programming Experience
1-3
Hey im wondering if there is any way to display a users ip in textbox1 when form loads. Thanks :) Im using vb.net 2012 which is similar to 2008 and 2010
 
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
 
Back
Top