【问题标题】:Get IP Address, Subnet, Default Gateway, DNS1 and DNS2 with VB.Net使用 VB.Net 获取 IP 地址、子网、默认网关、DNS1 和 DNS2
【发布时间】:2016-11-26 16:12:24
【问题描述】:

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

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 还不是很熟悉。

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    你应该使用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
    

    【讨论】:

    • 这非常有效。谢谢你。我一直在尝试各种不同的东西,我发现的所有教程和其他东西并不是我想要的,或者使用的东西比如组合框和没有转化为我想要的东西使用此信息。我只想单击一个按钮并让它显示网络堆栈。您的代码为此目的完美运行。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 2011-07-19
    • 1970-01-01
    • 2018-11-24
    • 2014-05-28
    相关资源
    最近更新 更多