VB.Net 返回 IPv4 地址

发布于 2024-09-03 07:16:14 字数 54 浏览 10 评论 0原文

如何在 VB.Net 中返回 IPv4 地址?

例如。 192.168.1.5

How can I return the IPv4 address in VB.Net?

eg. 192.168.1.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2024-09-10 07:16:14

像这样的东西

Public Function GetIpV4() As String

  Dim myHost As String = Dns.GetHostName
  Dim ipEntry As IPHostEntry = Dns.GetHostEntry(myHost)
  Dim ip As String = ""

  For Each tmpIpAddress As IPAddress In ipEntry.AddressList
     If tmpIpAddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
        Dim ipAddress As String = tmpIpAddress.ToString
        ip = ipAddress
        exit for
     End If
  Next

  If ip = "" Then
     Throw New Exception("No 10. IP found!")
  End If

  Return ip

End Function

Something like this

Public Function GetIpV4() As String

  Dim myHost As String = Dns.GetHostName
  Dim ipEntry As IPHostEntry = Dns.GetHostEntry(myHost)
  Dim ip As String = ""

  For Each tmpIpAddress As IPAddress In ipEntry.AddressList
     If tmpIpAddress.AddressFamily = Sockets.AddressFamily.InterNetwork Then
        Dim ipAddress As String = tmpIpAddress.ToString
        ip = ipAddress
        exit for
     End If
  Next

  If ip = "" Then
     Throw New Exception("No 10. IP found!")
  End If

  Return ip

End Function
滥情稳全场 2024-09-10 07:16:14

我能做的就是只返回 IPv4 地址
仅使用数组函数和 lambda 表达式,非常干净:

Public Function GetHostEntryIPv4(ByVal addr As String) As IPHostEntry

    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(addr)

    If Not IsNothing(ipHostInfo) Then
        ipHostInfo.AddressList = Array.FindAll(ipHostInfo.AddressList, Function(n As IPAddress) n.AddressFamily = AddressFamily.InterNetwork)
    End If

    GetHostEntryIPv4 = ipHostInfo

End Function

Best I can do is that, will return only IPv4 address
just using array functions and lambda expressions, very clean :

Public Function GetHostEntryIPv4(ByVal addr As String) As IPHostEntry

    Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(addr)

    If Not IsNothing(ipHostInfo) Then
        ipHostInfo.AddressList = Array.FindAll(ipHostInfo.AddressList, Function(n As IPAddress) n.AddressFamily = AddressFamily.InterNetwork)
    End If

    GetHostEntryIPv4 = ipHostInfo

End Function
戏剧牡丹亭 2024-09-10 07:16:14
Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()

编辑:

那么您可以使用 IPAddress .AddressFamily 找出 IP 系列类型。

Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName())
Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString()

edit:

then you can use IPAddress.AddressFamily to find out IP familly type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文