Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I need some reference to get the full TCP/IP details. Currently I'm able to get the IP Address, Subnet Mask and Gateway. But I'm not able to get the DNS. The value from DNS is able to get but to display is failed. Below are my code. I'm running on VB.Net Windows Form Application 2008. Please have a try and help me to find this.

VB
Imports System.Net
Imports System.Net.NetworkInformation

Public Class Form1
    Private strEvents As String
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Interfaces As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim adapter As NetworkInterface

        Dim myAdapterProps As IPInterfaceProperties = Nothing
        Dim myGateways As GatewayIPAddressInformationCollection = Nothing


        For Each adapter In Interfaces
            If adapter.NetworkInterfaceType = NetworkInterfaceType.Loopback Then
                Continue For
            End If
            TextBox1.AppendText(adapter.Name & Environment.NewLine)
            TextBox1.AppendText(adapter.Description & Environment.NewLine)
            myAdapterProps = adapter.GetIPProperties
            myGateways = myAdapterProps.GatewayAddresses
            Dim IPInfo As UnicastIPAddressInformationCollection = adapter.GetIPProperties().UnicastAddresses
            Dim properties As IPInterfaceProperties = adapter.GetIPProperties()

            For Each IPAddressInfo As UnicastIPAddressInformation In IPInfo
                TextBox1.AppendText("IP Address : " & IPAddressInfo.Address.ToString & Environment.NewLine)
                TextBox1.AppendText("Subnet Mask :" & IPAddressInfo.IPv4Mask.ToString & Environment.NewLine)
            Next

            For Each Gateway As GatewayIPAddressInformation In myGateways
                TextBox1.AppendText("Gateway IP :" & Gateway.Address.ToString & Environment.NewLine)
            Next

            TextBox1.AppendText("DNS Address :" & properties.DnsAddresses.ToString & Environment.NewLine)
        Next
    End Sub
End Class


Breakpoint on displaying DNS address and hover at "properties.DnsAddresses.ToString" at DnsAddresses. At Result, there are the DNS address which I want to display. Can someone help me?
Posted

1 solution

The "s" at the end of IPInterfaceProperties.DnsAddresses is a clue that there may be more than one, and the MSDN documentation backs that up: http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ipinterfaceproperties.dnsaddresses(v=vs.110).aspx[^]

It's a Collection: just calling ToString on it will not return all entries, any more than just looking at your iPod will list all the MP3 files directly into your brain - you have to iterate through the collection and look at each entry. Try another For Each loop: the link provides an example.
 
Share this answer
 
Comments
Luiey Ichigo 12-Nov-13 4:45am    
Thank you Mr. Griff,

It is all good
OriginalGriff 12-Nov-13 5:04am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900