IPInterfaceProperties.GatewayAddresses Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets the IPv4 network gateway addresses for this interface.

Namespace:   System.Net.NetworkInformation
Assembly:  System (in System.dll)

public abstract GatewayIPAddressInformationCollection GatewayAddresses { get; }

Property Value

Type: System.Net.NetworkInformation.GatewayIPAddressInformationCollection

An GatewayIPAddressInformationCollection that contains the address information for network gateways, or an empty array if no gateways are found.

Gateways that implement the Internet Protocol Version 4 (IPv4) to forward packets between networks are returned by this property.

The following code example displays the gateway addresses for the network interfaces on the local computer.

public static void DisplayGatewayAddresses()
{
    Console.WriteLine("Gateways");
    NetworkInterface[] adapters  = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface adapter in adapters)
    {
        IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
        GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses;
        if (addresses.Count >0)
        {
            Console.WriteLine(adapter.Description);
            foreach (GatewayIPAddressInformation address in addresses)
            {
                Console.WriteLine("  Gateway Address ......................... : {0}", 
                    address.Address.ToString());
            }
            Console.WriteLine();
        }
    }
}

.NET Framework
Available since 2.0
Return to top
Show: