I'm trying to get the ip address of my local PC, and one one of my other PCs it gets the v4 address fine, but on this one the code:
Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString()
returns what I guess is a IPv6 add开发者_JAVA百科ress:
fe80::9c09:e2e:4736:4c62%11
How do I get the IPv4 address?
Disclaimer- I don't have IPv6 installed and there is probably a much better way to do this, but what does the following return:
Dns.GetHostEntry(Dns.GetHostName()).AddressList
.Where(a => !a.IsIPv6LinkLocal && !a.IsIPv6Multicast && !a.IsIPv6SiteLocal)
.First()
.ToString();
Edit - didn't notice you were asking in VB, so I've tried translating it to:
Dim s As String = Dns.GetHostEntry(Dns.GetHostName()).AddressList _
.Where(Function(a As IPAddress) Not a.IsIPv6LinkLocal AndAlso Not a.IsIPv6Multicast AndAlso Not a.IsIPv6SiteLocal) _
.First() _
.ToString()
This may blow up, so don't treat it as production code.
Here's my solution for getting a routable IPv4 IP without using an external service:
Function GetLocalIP() As String
Dim IPList As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName)
For Each IPaddress In IPList.AddressList
'Only return IPv4 routable IPs
If (IPaddress.AddressFamily = Sockets.AddressFamily.InterNetwork) AndAlso (Not IsPrivateIP(IPaddress.ToString)) Then
Return IPaddress.ToString
End If
Next
Return ""
End Function
Function IsPrivateIP(ByVal CheckIP As String) As Boolean
Dim Quad1, Quad2 As Integer
Quad1 = CInt(CheckIP.Substring(0, CheckIP.IndexOf(".")))
Quad2 = CInt(CheckIP.Substring(CheckIP.IndexOf(".") + 1).Substring(0, CheckIP.IndexOf(".")))
Select Case Quad1
Case 10
Return True
Case 172
If Quad2 >= 16 And Quad2 <= 31 Then Return True
Case 192
If Quad2 = 168 Then Return True
End Select
Return False
End Function
Note that my code is also verifying that the range is routable (IsPrivateIP). You can remove or modify that part if you are looking for something else.
I used a combined Cmd/Visual Basic code and it worked :
Dim ResString As String = "result.txt"
If File.Exists("result.txt") Then
File.Delete("result.txt")
End If
Shell("cmd.exe /c cd " & Application.StartupPath & " && ipconfig >> " & ResString & "&& exit", AppWinStyle.NormalFocus)
Dim Ipv4 As String
Dim Ipv4Found As Boolean = False
Dim Ipv4Char As Integer = 43
Dim Ipv4Str As String
Threading.Thread.Sleep(1500)
'Wait some seconds to create "result.txt"
Dim Ipv4Reader As StreamReader
Ipv4Reader = File.OpenText("result.txt")
Do Until Ipv4Found = True
Ipv4Str = Ipv4Reader.ReadLine()
If Not Ipv4Str = Nothing Then
If Ipv4Str.Contains("IPv4") Then
Try
Ipv4 = Ipv4Str.Chars(Ipv4Char)
Do Until Ipv4Char = 60
Ipv4Char = Ipv4Char + 1
Ipv4 = Ipv4 & Ipv4Str.Chars(Ipv4Char)
'Read results step by step
Loop
Catch ex As Exception
End Try
MsgBox("Your IPv4 Address is " & Ipv4)
Ipv4Found = True
Ipv4Reader.Close()
End If
Else
End If
Loop
If your computer language is english you may have some unusual characters in the IPv4 String ( My pc is actually in Italian )
I think you should use this:
Dim tmpHostName As String = System.Net.Dns.GetHostName()
myIPaddress = System.Net.Dns.GetHostByName(tmpHostName).AddressList(0).ToString()
GetHostByName
is obsolete but this is the way to get the IPv4. Why? Because the getbyhostname
function is created before IPv6 so the function get only the IPv4 connection, not the fe80::9c09:e2e:4736:4c62%11
.
Something maybe fun is this little function that'll show all IP addresses on your computer:
Public Function getOwnIp() As String
Dim hostIP As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim position As Integer = 0
Dim ip As String = Nothing
While ipList < hostIP.AddressList.Length
ip += hostIP.AddressList(position).ToString & vbCrLf
position += 1
End While`enter code here`
Return ip
End Function
I was looking for the answer to this question myself and I could not find one suitable to my needs. I managed to experiment with various answers across the net until I came up with this (works great!). Just thought I would share since this post is the top result via Google.
''''Routine to fetch IPv4 Network addresses for all local network interfaces.
Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
Dim adapter As NetworkInterface
For Each adapter In adapters
Dim properties As IPInterfaceProperties = adapter.GetIPProperties()
If properties.UnicastAddresses.Count > 0 Then
For Each unicastadress As UnicastIPAddressInformation In properties.UnicastAddresses
Dim ip As IPAddress = unicastadress.Address
If ip.AddressFamily = AddressFamily.InterNetwork Then
ComboBox1.Items.Add(ip.ToString)
End If
Next unicastadress
End If
Next adapter
You first need to import the system namespace into your application and then create an instance of the System.Net.NetworkInformation.IPAddressInformation
and use it as such
Example
Imports system.data.sqlclient
imports system
Public class Form1
Dim IPAdd As System.Net.NetworkInformation.IPAddressInformation
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("My IP Address is " & IPAdd.Address.ToString)
End Sub
End Class
Dim localIp As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
txtLocal.Text = localIp.AddressList(1).ToString
Notice that I changed the (0)
index to (1)
.
This one works on my side
Dim IPaddressList = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList
Dim IPaddrPC As String = ""
For Each item In IPaddressList
If item.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
IPaddrPC = item.Address.ToString
Exit For
End If
Next
精彩评论