Get the IP and Computer Name

9.1k Views Asked by At

I have this code:

localIp = Request.UserHostName
hostName = DetermineCompName(localIp)

Session.Add("localIp", localIp)
Session.Add("hostName", hostName)

As you can see, I put the 2 variables on a session so that I can use it when I want. Testing the app on 10 computers, I saw that on some of the computers it gets the Client IP and the Computer name, but on others it goes empty.

As in some computers it works, I don't understand what's wrong. Does anyone have the right method to do this?

1

There are 1 best solutions below

0
On

To get the computer name you can simply do:

Dim hostName As String = Environment.MachineName 

or:

Dim hostName As String = My.Computer.Name

For the IP it's a little bit trickier, I assume you want the ipV4, so you can try this:

Dim localIp As String
For Each address As System.Net.IPAddress In System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList
    If address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
        localIp = address.ToString()
        Exit For
    End If
Next

Please note that if you do just:

System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName).AddressList(0).ToString()

then this will return the ipV6.