IPNetworkInterface Class

Namespace: System.Net.NetworkInformation

Provides properties and methods to query and manage network interfaces on the local computer.

Summary

The IPNetworkInterface class is a fundamental part of the .NET Framework's networking capabilities. It allows developers to programmatically access information about the network interfaces present on a machine. This includes details such as the interface's name, description, MAC address, IP addresses assigned to it, status (up or down), and operational state.

By using IPNetworkInterface, applications can dynamically discover network configurations, monitor interface status, and perform operations that require knowledge of the local network environment. This is crucial for applications involved in network configuration, diagnostics, routing, and any scenario requiring an understanding of how the computer is connected to various networks.

Syntax


public abstract class IPNetworkInterface : object

Methods

Properties

Example Usage

The following example demonstrates how to retrieve all network interfaces on the local computer and display their basic information:


using System;
using System.Net.NetworkInformation;

public class NetworkInfoExample
{
    public static void Main(string[] args)
    {
        IPNetworkInterface[] networkInterfaces = IPNetworkInterface.GetAllNetworkInterfaces();

        Console.WriteLine("Network Interfaces on this computer:");
        Console.WriteLine("-----------------------------------");

        foreach (IPNetworkInterface adapter in networkInterfaces)
        {
            Console.WriteLine($"Name: {adapter.Name}");
            Console.WriteLine($"Description: {adapter.Description}");
            Console.WriteLine($"Type: {adapter.NetworkInterfaceType}");
            Console.WriteLine($"Status: {adapter.OperationalStatus}");
            Console.WriteLine($"MAC Address: {adapter.GetPhysicalAddress()}");
            Console.WriteLine($"Speed: {adapter.Speed} bps");

            IPInterfaceProperties ipProperties = adapter.GetIPProperties();
            Console.WriteLine("  IP Addresses:");
            foreach (UnicastIPAddressInformation ipAddressInfo in ipProperties.UnicastAddresses)
            {
                Console.WriteLine($"    - {ipAddressInfo.Address}");
            }

            Console.WriteLine();
        }
    }
}
        

Related Topics