NetworkInterface 類別

定義

提供網路介面的組態和統計資訊。Provides configuration and statistical information for a network interface.

public ref class NetworkInterface abstract
public abstract class NetworkInterface
type NetworkInterface = class
Public MustInherit Class NetworkInterface
繼承
NetworkInterface

範例

下列程式碼範例會顯示介面的相關資訊。The following code example displays information about interfaces.

void ShowNetworkInterfaces()
{
   IPGlobalProperties ^ computerProperties = IPGlobalProperties::GetIPGlobalProperties();
   array<NetworkInterface^>^nics = NetworkInterface::GetAllNetworkInterfaces();
   Console::WriteLine( "Interface information for {0}.{1}     ", computerProperties->HostName, computerProperties->DomainName );
   if ( nics == nullptr || nics->Length < 1 )
   {
      Console::WriteLine( "  No network interfaces found." );
      return;
   }

   Console::WriteLine( "  Number of interfaces .................... : {0}", nics->Length );
   System::Collections::IEnumerator^ myEnum4 = nics->GetEnumerator();
   while ( myEnum4->MoveNext() )
   {
      NetworkInterface ^ adapter = safe_cast<NetworkInterface ^>(myEnum4->Current);
      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() );
      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 = String::Concat( versions, " " );
         }
         versions = String::Concat( 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();
   }
}
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();
    }

備註

此類別會封裝本機電腦上網路介面(也稱為介面卡)的資料。This class encapsulates data for network interfaces, also known as adapters, on the local computer. 您不會建立此類別的實例;GetAllNetworkInterfaces 方法會針對本機電腦上的每個網路介面,傳回一個陣列,其中包含這個類別的一個實例。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.

建構函式

NetworkInterface()

NetworkInterface 類別的新執行個體初始化。Initializes a new instance of the NetworkInterface class.

屬性

Description

取得介面的描述。Gets the description of the interface.

Id

取得網路介面卡的識別項。Gets the identifier of the network adapter.

IPv6LoopbackInterfaceIndex

取得 IPv6 回送介面的索引。Gets the index of the IPv6 loopback interface.

IsReceiveOnly

取得 Boolean 值,指出網路介面是否設定為只接收資料封包。Gets a Boolean value that indicates whether the network interface is set to only receive data packets.

LoopbackInterfaceIndex

取得 IPv4 回送介面的索引。Gets the index of the IPv4 loopback interface.

Name

取得網路介面卡的名稱。Gets the name of the network adapter.

NetworkInterfaceType

取得介面類型。Gets the interface type.

OperationalStatus

取得網路連接的目前操作狀態。Gets the current operational state of the network connection.

Speed

取得網路介面的速度。Gets the speed of the network interface.

SupportsMulticast

取得 Boolean 值,指出網路介面是否啟用為可接收多點傳送封包。Gets a Boolean value that indicates whether the network interface is enabled to receive multicast packets.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetAllNetworkInterfaces()

傳回物件,其描述本機電腦上的網路介面。Returns objects that describe the network interfaces on the local computer.

GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetIPProperties()

傳回物件,其描述這個網路介面的組態。Returns an object that describes the configuration of this network interface.

GetIPStatistics()

取得這個 NetworkInterface 執行個體的 IP 統計資料。Gets the IP statistics for this NetworkInterface instance.

GetIPv4Statistics()

取得這個 NetworkInterface 執行個體的 IPv4 統計資料。Gets the IPv4 statistics for this NetworkInterface instance.

GetIsNetworkAvailable()

指出是否有任何網路連接可使用。Indicates whether any network connection is available.

GetPhysicalAddress()

傳回這個介面卡的媒體存取控制 (MAC) 或實體位址。Returns the Media Access Control (MAC) or physical address for this adapter.

GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
Supports(NetworkInterfaceComponent)

取得 Boolean 值,指出介面是否支援指定的通訊協定。Gets a Boolean value that indicates whether the interface supports the specified protocol.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於