gpt4 book ai didi

vb.net - 在 VB.NET 中获取本地 IP 地址 (IPV4) - 错误 "Resolve is obsoleted for this type"较新版本

转载 作者:行者123 更新时间:2023-12-05 03:56:25 27 4
gpt4 key购买 nike

我这里有显示主机 IP 地址的代码,但它给我一个错误。我该如何解决这个问题?

并摆脱 Resolve is obsoted for this type 东西。

错误信息


Description
-----------------------------------------------------------------
Public Shared Overloads Function Resolve(hostName As String)
As IPHostEntry' is obsolete:

Resolve is obsoleted for this type. Please use GetHostEntry instead.

http://go.microsoft.com/fwlink/?linkid=14202'


Severity Code Project Line Suppression State
-----------------------------------------------------------------
Warning BC40000 UltimateDiskless 270 Active

File
-----
C:\Users\Jadam\Desktop\Ultimate Diskless\UltimateDiskless\UltimateDiskless\PacketScanner\frmMain.vb

代码

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim hosts() As IPAddress = Dns.Resolve(Dns.GetHostName()).AddressList <==**This One Has The error**

Dim i As Integer

If hosts.Length = 0 Then Throw New NotSupportedException("This computer does not have non-loopback interfaces installed!")
For i = 0 To hosts.Length - 1
MonitorMenuItem.MenuItems.Add(hosts(i).ToString(), New EventHandler(AddressOf Me.OnHostsClick))
HostsMenu.MenuItems.Add(hosts(i).ToString(), New EventHandler(AddressOf Me.OnHostsClick))
Next

ReDim m_PacketMonitors(HostsMenu.MenuItems.Count - 1)

For i = 0 To m_PacketMonitors.Length - 1
m_PacketMonitors(i) = New Monitor(hosts(i))
AddHandler m_PacketMonitors(i).NewPacket, AddressOf Me.OnNewPacket
Next
m_Packets = New ArrayList

End Sub

最佳答案

本例中的错误清楚地告诉您如何解决:

Resolve is obsoleted for this type, please use GetHostEntry instead

但是,由于您确实需要 IP 地址,因此现在使用 Dns.GetHostAddresses 获取 IP 地址的正确方法:

Dim hosts() As IPAddress = Dns.GetHostAddresses(Dns.GetHostName())

GetHostAddresses 的文档是 here .

编辑以下关于 IPv4 地址的 OP 评论

您可以使用 hosts() 中的结果通过以下代码查找 IPv4 地址:

For Each address As IPAddress In hosts
If address.AddressFamily = AddressFamily.InterNetwork Then
Console.WriteLine($"{address} is IPv4")
End If
Next

AddressFamilySystem.Net.Sockets 命名空间(如果您尚未导入它)。

关于vb.net - 在 VB.NET 中获取本地 IP 地址 (IPV4) - 错误 "Resolve is obsoleted for this type"较新版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59323079/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com