I am using this to get my local IPV4 in VB but the thing is that I have 4 networks adapters, so it returns the wrong IP. The right one is on the third nic. But if I hardcode "addressList(2)" to get the third value, my code won't be universal... How is it possible to get the right IPV4 (the one that is linked with the nic that has an internet connection) on every computer ? Thanks.
Dim entry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName)
Dim addressList As New List(Of IPAddress)
For Each address In entry.AddressList
If address.AddressFamily = AddressFamily.InterNetwork Then
addressList.Add(address)
MsgBox(address.ToString)
End If
Next
Dim interip As String = addressList(0).ToString
>>282431
That's the original code, so it's easier in case you have the solution
>>282429
>I have 4 networks adapters, so it returns the wrong IP.
What's wrong with it?
>>282434
It's not connected to Internet nor to a router. All the other are like VMWare nics
>>282451
It's not used or anything
how do I check for this with NetworkInterface ?
>>282508
how can I check if there's connectivity on a specific nic ?
>>282510
same way windows does, ping an outside ip (microsoft) using that connection
>>282531
fuck, me forgot link related
>http://blog.superuser.com/2011/05/16/windows-7-network-awareness/
>>282537
how ?
but in the end I used:
Dim nics() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
For Each nic In nics
If nic.GetIPStatistics.BytesReceived > 50000 And nic.GetIPProperties.UnicastAddresses.Count > 0 And nic.OperationalStatus = OperationalStatus.Up And Not nic.NetworkInterfaceType = NetworkInterfaceType.Loopback And Not nic.IsReceiveOnly Then
For Each ip In nic.GetIPProperties.UnicastAddresses
If ip.Address.AddressFamily = AddressFamily.InterNetwork Then
End If
Next
End If
Next