NetworkInterface Klasa

Definicja

Udostępnia informacje o konfiguracji i statystyczne interfejsie sieciowym.

public ref class NetworkInterface abstract sealed
public ref class NetworkInterface abstract
public static class NetworkInterface
public abstract class NetworkInterface
type NetworkInterface = class
Public Class NetworkInterface
Public MustInherit Class NetworkInterface
Dziedziczenie
NetworkInterface

Przykłady

Poniższy przykład kodu przedstawia informacje o interfejsach.

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();
    }
}

Uwagi

Ta klasa hermetyzuje dane dla interfejsów sieciowych, znanych również jako karty, na komputerze lokalnym. Nie należy tworzyć wystąpień tej klasy; metoda GetAllNetworkInterfaces zwraca tablicę zawierającą jedno wystąpienie tej klasy dla każdego interfejsu sieciowego na komputerze lokalnym.

Konstruktory

NetworkInterface()

Inicjuje nowe wystąpienie klasy NetworkInterface.

Właściwości

Description

Pobiera opis interfejsu.

Id

Pobiera identyfikator karty sieciowej.

IPv6LoopbackInterfaceIndex

Pobiera indeks interfejsu sprzężenia zwrotnego IPv6.

IsReceiveOnly

Pobiera wartość wskazującą Boolean , czy interfejs sieciowy jest ustawiony na odbieranie tylko pakietów danych.

LoopbackInterfaceIndex

Pobiera indeks interfejsu sprzężenia zwrotnego IPv4.

Name

Pobiera nazwę karty sieciowej.

NetworkInterfaceType

Pobiera typ interfejsu.

OperationalStatus

Pobiera bieżący stan operacyjny połączenia sieciowego.

Speed

Pobiera szybkość interfejsu sieciowego.

SupportsMulticast

Pobiera wartość wskazującą Boolean , czy interfejs sieciowy jest włączony do odbierania pakietów multiemisji.

Metody

GetAllNetworkInterfaces()

Zwraca obiekty opisujące interfejsy sieciowe na komputerze lokalnym.

GetIPProperties()

Zwraca obiekt opisujący konfigurację tego interfejsu sieciowego.

GetIPStatistics()

Pobiera statystyki adresów IP dla tego NetworkInterface wystąpienia.

GetIPv4Statistics()

Pobiera statystyki IPv4 dla tego NetworkInterface wystąpienia.

GetIsNetworkAvailable()

Wskazuje, czy jakiekolwiek połączenie sieciowe jest dostępne.

GetPhysicalAddress()

Zwraca Access Control multimediów (MAC) lub adres fizyczny dla tej karty.

Supports(NetworkInterfaceComponent)

Pobiera wartość wskazującą Boolean , czy interfejs obsługuje określony protokół.

Dotyczy