IPConfiguration Class

Namespace: System.Net.IP

Assembly: System.Net.Sockets.dll

Introduction

The IPConfiguration class provides static methods for retrieving and configuring IP network settings on a local machine. This class is part of the System.Net.IP namespace, offering programmatic access to network interface information, IP addresses, subnet masks, and default gateways.

Properties

The IPConfiguration class primarily exposes static methods rather than instance properties. These methods allow you to query the network configuration without needing to instantiate the class.

Methods

static IPAddressCollection GetAllIPv4Addresses()

Retrieves a collection of all IPv4 addresses configured on the local machine.


using System.Net;
using System.Net.IP;

IPAddressCollection ipv4Addresses = IPConfiguration.GetAllIPv4Addresses();
foreach (IPAddress address in ipv4Addresses)
{
    Console.WriteLine($"IPv4 Address: {address}");
}
            

static IPAddressCollection GetAllIPv6Addresses()

Retrieves a collection of all IPv6 addresses configured on the local machine.


using System.Net;
using System.Net.IP;

IPAddressCollection ipv6Addresses = IPConfiguration.GetAllIPv6Addresses();
foreach (IPAddress address in ipv6Addresses)
{
    Console.WriteLine($"IPv6 Address: {address}");
}
            

static IPAddress GetDefaultGateway()

Retrieves the default gateway IP address for the local machine.


using System.Net;
using System.Net.IP;

IPAddress defaultGateway = IPConfiguration.GetDefaultGateway();
if (defaultGateway != null)
{
    Console.WriteLine($"Default Gateway: {defaultGateway}");
}
else
{
    Console.WriteLine("Default gateway not found.");
}
            

static NetworkInterface[] GetAllNetworkInterfaces()

Retrieves an array of all network interfaces present on the local machine. Each NetworkInterface object contains detailed information about an interface.


using System.Net.NetworkInformation;
using System.Net.IP;

NetworkInterface[] interfaces = IPConfiguration.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
    Console.WriteLine($"Interface: {ni.Name}");
    Console.WriteLine($"  Description: {ni.Description}");
    Console.WriteLine($"  Operational Status: {ni.OperationalStatus}");
    // Further details can be accessed from the NetworkInterface object
}
            

Example Usage: Retrieving Detailed Interface Information

This example demonstrates how to iterate through network interfaces and display their associated IP properties.


using System.Net;
using System.Net.NetworkInformation;
using System.Net.IP;

NetworkInterface[] interfaces = IPConfiguration.GetAllNetworkInterfaces();

foreach (NetworkInterface ni in interfaces)
{
    Console.WriteLine($"--- Interface: {ni.Name} ---");
    Console.WriteLine($"  Description: {ni.Description}");
    Console.WriteLine($"  MAC Address: {ni.GetPhysicalAddress().ToString()}");
    Console.WriteLine($"  Operational Status: {ni.OperationalStatus}");
    Console.WriteLine($"  Is Up: {ni.IsReceiveOnly ? "No" : "Yes"}"); // Simplified check

    IPInterfaceProperties ipProperties = ni.GetIPProperties();

    Console.WriteLine("  IPv4 Properties:");
    if (ipProperties.IPv4AddressConfiguration != null)
    {
        Console.WriteLine($"    DHCP Enabled: {ipProperties.IPv4AddressConfiguration.DhcpEnabled}");
        Console.WriteLine($"    Is Automatic (APIPA): {ipProperties.IPv4AddressConfiguration.IsAutomatic}");
    }

    Console.WriteLine("  IPv4 Addresses:");
    foreach (UnicastIPAddressInformation ipAddressInfo in ipProperties.UnicastAddresses)
    {
        if (ipAddressInfo.Address.AddressFamily == AddressFamily.InterNetwork)
        {
            Console.WriteLine($"    Address: {ipAddressInfo.Address}");
            Console.WriteLine($"    Subnet Mask: {ipAddressInfo.IPv4Mask}");
        }
    }

    Console.WriteLine("  IPv6 Addresses:");
    foreach (UnicastIPAddressInformation ipAddressInfo in ipProperties.UnicastAddresses)
    {
        if (ipAddressInfo.Address.AddressFamily == AddressFamily.InterNetworkV6)
        {
            Console.WriteLine($"    Address: {ipAddressInfo.Address}");
        }
    }

    Console.WriteLine("  Gateway Addresses:");
    foreach (GatewayIPAddressInformation gatewayInfo in ipProperties.GatewayAddresses)
    {
        Console.WriteLine($"    Gateway: {gatewayInfo.Address}");
    }
    Console.WriteLine();
}
            
Tip: For more granular control over network interfaces and their properties, consider using the classes within the System.Net.NetworkInformation namespace directly, such as NetworkInterface and IPInterfaceProperties.
Note: The availability and behavior of these methods may vary slightly depending on the operating system and its network configuration. Ensure you have the necessary permissions to access network information.

Related Topics