NetworkInterface Class
Namespace: System.Net.NetworkInformation
Assembly: System
Summary
Provides information about network interfaces on the local computer.
Syntax
public abstract class NetworkInterface
Remarks
The NetworkInterface
class allows you to retrieve detailed information about all network interfaces installed on a computer, including their status, type, IP addresses, MAC addresses, and performance statistics.
You can use the static GetAllNetworkInterfaces
method to get an array of all NetworkInterface
objects available on the system.
Methods
-
GetAllNetworkInterfaces()
Retrieves all network interface information for the local computer.
-
GetIPProperties()
Retrieves the IP properties for the network interface.
-
GetIPStatistics()
Retrieves the IP statistics for the network interface.
-
IsReceiveOnly
Indicates whether the network interface is configured to receive packets only.
Properties
-
Id
Gets the identifier of the network interface.
-
Name
Gets the name of the network interface.
-
OperationalStatus
Gets the operational status of the network interface.
-
NetworkInterfaceType
Gets the type of the network interface.
Example
The following example iterates through all network interfaces on the local computer and displays their names, types, and operational status.
using System;
using System.Net.NetworkInformation;
public class NetworkInfoExample
{
public static void Main(string[] args)
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Network Interfaces:");
Console.WriteLine("-----------------");
foreach (NetworkInterface ni in interfaces)
{
Console.WriteLine($"Name: {ni.Name}");
Console.WriteLine($" Description: {ni.Description}");
Console.WriteLine($" Type: {ni.NetworkInterfaceType}");
Console.WriteLine($" Status: {ni.OperationalStatus}");
Console.WriteLine($" MAC Address: {ni.GetPhysicalAddress().ToString()}");
IPInterfaceProperties ipProps = ni.GetIPProperties();
if (ipProps != null)
{
Console.WriteLine($" IP Address(es):");
foreach (UnicastIPAddressInformation ip in ipProps.UnicastAddresses)
{
Console.WriteLine($" - {ip.Address}");
}
}
Console.WriteLine();
}
}
}