user1837575 user1837575 - 1 year ago 179 Question

Get IP Address, Subnet, Default Gateway, DNS1 and DNS2 with VB.Net

I want to click a button that updates multiple text labels or multiple text boxes with the network stack information. Here is the logic I'm after.

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

I have some working code

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

I'm not sure how to get the default gateway or DNS's. The Subnet Mask isn't that important for what I'm trying to do but gateway and DNS entries are important.

I just want to click one button and have it show me a nicely formatted network stack. It doesn't seem like this should be that hard but I'm not that familiar with yet.

Answer Source

You should use NetworkInterface class. It contains all informations what you want. You can get detailed information here:

Simple usage:

'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()

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

                        If adapterProperties.DnsAddresses.Count > 1 Then
                            Label6.Text = adapterProperties.DnsAddresses(1).ToString()
                        End If
                    End If
                End If
    End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download