Retrieving gateway addresses from System.Net.NetworkInformation





    After my previous thread about retrieving the ip details of the local machine, Jo0ls told me to look at the system.net.networkinformation namespace to do this. However, im still unable to retrieve my gateway adress in vb 2008 express.


    I used my own code to try and display my local gateway address which produced an output of:




    I couldnt find the problem so I used MSDNs code of:



    Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()

    Dim adapter As NetworkInterface

    For Each adapter In adapters

    Dim adapterProperties As IPInterfaceProperties = adapter.GetIPProperties()

    Dim addresses As GatewayIPAddressInformationCollection = adapterProperties.GatewayAddresses

    If addresses.Count > 0 Then


    Dim address As GatewayIPAddressInformation

    For Each address In addresses

    Console.WriteLine(" Gateway Address ......................... : {0}", address.ToString())

    Next address


    End If

    Next adapter


    this produced an ouput of:



    WAN (PPP/SLIP) Interface

    Gateway Address ......................... : System.Net.NetworkInformation.SystemGatewayIPAddressInformation



    Is this the right coe for displaying your IP gateway? Can somebody please tell me whats going on here?





    Monday, August 13, 2007 9:56 PM


  • Hi Paulio,


    Based on your post, I know that the upper code doesn't get the GateWay information. Please try the following code snippet to get GateWay information. If you have any further questions about VB 2008 Express. Please post your issues on Visual Basic Express Orcas forum. This forum is specially for handling Visual Basic Express Orcas issues. You can get the better assistance to your issues. Hope this helps.  

    Code Snippet

    Imports System

    Imports System.Collections.Generic

    Imports System.Text

    Imports System.Net

    Imports System.Net.NetworkInformation

    Module Module1

        Sub Main()



        End Sub

        Public Sub ShowNetworkInformation()

            Dim ipProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()

            Console.WriteLine("Host name: {0}", ipProperties.HostName)

            Console.WriteLine("Domain name: {0}", ipProperties.DomainName)

            For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()

                Console.WriteLine("Interface: {0}", networkCard.Id)

                Console.WriteLine("" & Chr(9) & " Name: {0}", networkCard.Name)

                Console.WriteLine("" & Chr(9) & " Description: {0}", networkCard.Description)

                Console.WriteLine("" & Chr(9) & " Status: {0}", networkCard.OperationalStatus)

                Console.WriteLine("" & Chr(9) & " MAC Address: {0}", networkCard.GetPhysicalAddress().ToString())


                Console.WriteLine("" & Chr(9) & " Gateway Address:")

                For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties().GatewayAddresses

                    Console.WriteLine("" & Chr(9) & "" & Chr(9) & " Gateway entry: {0}", gatewayAddr.Address.ToString())


                Console.WriteLine("" & Chr(9) & " DNS Settings:")

                For Each address As IPAddress In networkCard.GetIPProperties().DnsAddresses

                    Console.WriteLine("" & Chr(9) & "" & Chr(9) & " DNS entry: {0}", address.ToString())



        End Sub

    End Module





    Thanks for your questions.


    Thursday, August 16, 2007 5:10 AM