Export (0) Print
Expand All

NetworkInterface Class

Provides configuration and statistical information for a network interface.

System.Object
  System.Net.NetworkInformation.NetworkInterface

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

public abstract class NetworkInterface

The NetworkInterface type exposes the following members.

  NameDescription
Protected methodNetworkInterfaceInitializes a new instance of the NetworkInterface class.
Top

  NameDescription
Public propertyDescriptionGets the description of the interface.
Public propertyIdGets the identifier of the network adapter.
Public propertyIsReceiveOnlyGets a Boolean value that indicates whether the network interface is set to only receive data packets.
Public propertyStatic memberLoopbackInterfaceIndexGets the index of the IPv4 loopback interface.
Public propertyNameGets the name of the network adapter.
Public propertyNetworkInterfaceTypeGets the interface type.
Public propertyOperationalStatusGets the current operational state of the network connection.
Public propertySpeedGets the speed of the network interface.
Public propertySupportsMulticastGets a Boolean value that indicates whether the network interface is enabled to receive multicast packets.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberGetAllNetworkInterfacesReturns objects that describe the network interfaces on the local computer.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetIPPropertiesReturns an object that describes the configuration of this network interface.
Public methodGetIPv4StatisticsGets the IPv4 statistics.
Public methodStatic memberSupported by Portable Class LibraryGetIsNetworkAvailableIndicates whether any network connection is available.
Public methodGetPhysicalAddressReturns the Media Access Control (MAC) or physical address for this adapter.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupportsGets a Boolean value that indicates whether the interface supports the specified protocol.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

This class encapsulates data for network interfaces, also known as adapters, on the local computer. You do not create instances of this class; the GetAllNetworkInterfaces method returns an array that contains one instance of this class for each network interface on the local computer.

The following code example displays information about interfaces.


public static void ShowNetworkInterfaces()
{
    IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
    Console.WriteLine("Interface information for {0}.{1}     ",
            computerProperties.HostName, computerProperties.DomainName);
    if (nics == null || nics.Length < 1)
    {
        Console.WriteLine("  No network interfaces found.");
        return;
    }

    Console.WriteLine("  Number of interfaces .................... : {0}", nics.Length);
    foreach (NetworkInterface adapter in nics)
    {
        IPInterfaceProperties properties = adapter.GetIPProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.WriteLine("  Physical Address ........................ : {0}", 
                   adapter.GetPhysicalAddress().ToString());
        Console.WriteLine("  Operational status ...................... : {0}", 
            adapter.OperationalStatus);
        string versions ="";

        // Create a display string for the supported IP versions.
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
             versions = "IPv4";
         }
        if (adapter.Supports(NetworkInterfaceComponent.IPv6))
        {
            if (versions.Length > 0)
            {
                versions += " ";
             }
            versions += "IPv6";
        }
        Console.WriteLine("  IP version .............................. : {0}", versions);
        ShowIPAddresses(properties);

        // The following information is not useful for loopback adapters.
        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback)
        {
            continue;
        }
        Console.WriteLine("  DNS suffix .............................. : {0}", 
            properties.DnsSuffix);

        string label;
        if (adapter.Supports(NetworkInterfaceComponent.IPv4))
        {
            IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties();
            Console.WriteLine("  MTU...................................... : {0}", ipv4.Mtu);
            if (ipv4.UsesWins)
            {

                IPAddressCollection winsServers = properties.WinsServersAddresses;
                if (winsServers.Count > 0)
                {
                    label = "  WINS Servers ............................ :";
                    ShowIPAddresses(label, winsServers);
                }
            }
        }

        Console.WriteLine("  DNS enabled ............................. : {0}", 
            properties.IsDnsEnabled);
        Console.WriteLine("  Dynamically configured DNS .............. : {0}", 
            properties.IsDynamicDnsEnabled);
        Console.WriteLine("  Receive Only ............................ : {0}", 
            adapter.IsReceiveOnly);
        Console.WriteLine("  Multicast ............................... : {0}", 
            adapter.SupportsMulticast);
        ShowInterfaceStatistics(adapter);

        Console.WriteLine();
    }


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft