使用 VB.net 获取计算机的本地 IPv4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1642256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get local IPv4 of computer using VB.net
提问by Jonathan.
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:
我正在尝试获取我本地 PC 的 IP 地址,而我的另一台 PC 则可以获取 v4 地址,但在这台 PC 上,代码如下:
Dns.GetHostEntry(Dns.GetHostName).AddressList(0).ToString()
returns what I guess is a IPv6 address:
返回我猜是一个 IPv6 地址:
fe80::9c09:e2e:4736:4c62%11
How do I get the IPv4 address?
如何获取 IPv4 地址?
回答by RichardOD
Disclaimer- I don't have IPv6 installed and there is probably a much better way to do this, but what does the following return:
免责声明- 我没有安装 IPv6 并且可能有更好的方法来做到这一点,但以下返回什么:
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:
编辑 - 没有注意到您在 VB 中询问,所以我尝试将其翻译为:
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.
这可能会爆炸,所以不要把它当作生产代码。
回答by Ale865
I used a combined Cmd/Visual Basic code and it worked :
我使用了一个组合的 Cmd/Visual Basic 代码并且它有效:
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 )
如果您的计算机语言是英语,则 IPv4 字符串中可能有一些不寻常的字符(我的电脑实际上是意大利语)
回答by Bryan
Here's my solution for getting a routable IPv4 IP without using an external service:
这是我在不使用外部服务的情况下获取可路由 IPv4 IP 的解决方案:
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.
请注意,我的代码也在验证范围是否可路由(IsPrivateIP)。如果您正在寻找其他东西,您可以删除或修改该部分。
回答by choi
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
.
GetHostByName
已过时,但这是获取 IPv4 的方法。为什么?因为该getbyhostname
函数是在 IPv6 之前创建的,所以该函数只能获得 IPv4 连接,而不是fe80::9c09:e2e:4736:4c62%11
.
回答by Draeven
Something maybe fun is this little function that'll show all IP addresses on your computer:
这个小功能可能很有趣,它可以显示您计算机上的所有 IP 地址:
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
回答by user2995077
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
回答by user3846464
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
您首先需要将系统命名空间导入到您的应用程序中,然后创建一个实例System.Net.NetworkInformation.IPAddressInformation
并以此方式使用它
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
回答by lasq
Dim localIp As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
txtLocal.Text = localIp.AddressList(1).ToString
Notice that I changed the (0)
index to (1)
.
请注意,我将(0)
索引更改为(1)
.