gpt4 book ai didi

vb.net - 使用 VB.Net 获取 IP 地址、子网、默认网关、DNS1 和 DNS2

转载 作者:行者123 更新时间:2023-12-02 02:15:58 26 4
gpt4 key购买 nike

我想单击一个按钮,用网络堆栈信息更新多个文本标签或多个文本框。这是我所追求的逻辑。

Button2 event 
Label1.text = Computer Name
Label2.text = IP Address
Label3.text = Subnet Mask
Label4.text = Default Gateway
Label5.text = DNS1
Label6.text = DNS2

我有一些工作代码

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Dim strHostName As String

Dim strIPAddress As String


strHostName = System.Net.Dns.GetHostName()

strIPAddress = System.Net.Dns.GetHostByName(strHostName).AddressList(0).ToString()



TextBox2.Text = strIPAddress
Lable_IPAddress.Text = strIPAddress


End Sub

我不确定如何获取默认网关或 DNS。子网掩码对于我想做的事情来说并不那么重要,但网关和 DNS 条目很重要。

我只想单击一个按钮,让它向我显示格式良好的网络堆栈。看起来这应该不那么难,但我对 vb.net 还不太熟悉。

最佳答案

您应该使用NetworkInterface类。它包含您想要的所有信息。您可以在这里获取详细信息:

https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

简单用法:

'Computer Name
Label1.Text = System.Net.Dns.GetHostName()
For Each ip In System.Net.Dns.GetHostEntry(Label1.Text).AddressList
If ip.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
'IPv4 Adress
Label2.Text = ip.ToString()

For Each adapter As Net.NetworkInformation.NetworkInterface In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
For Each unicastIPAddressInformation As Net.NetworkInformation.UnicastIPAddressInformation In adapter.GetIPProperties().UnicastAddresses
If unicastIPAddressInformation.Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
If ip.Equals(unicastIPAddressInformation.Address) Then
'Subnet Mask
Label3.Text = unicastIPAddressInformation.IPv4Mask.ToString()

Dim adapterProperties As Net.NetworkInformation.IPInterfaceProperties = adapter.GetIPProperties()
For Each gateway As Net.NetworkInformation.GatewayIPAddressInformation In adapterProperties.GatewayAddresses
'Default Gateway
Label4.Text = gateway.Address.ToString()
Next

'DNS1
If adapterProperties.DnsAddresses.Count > 0 Then
Label5.Text = adapterProperties.DnsAddresses(0).ToString()
End If

'DNS2
If adapterProperties.DnsAddresses.Count > 1 Then
Label6.Text = adapterProperties.DnsAddresses(1).ToString()
End If
End If
End If
Next
Next
End If
Next

关于vb.net - 使用 VB.Net 获取 IP 地址、子网、默认网关、DNS1 和 DNS2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814462/

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