PhysicalAddress 類別

定義

提供網路介面 (介面卡) 的媒體存取控制 (MAC) 位址。Provides the Media Access Control (MAC) address for a network interface (adapter).

public ref class PhysicalAddress
public class PhysicalAddress
type PhysicalAddress = class
Public Class PhysicalAddress
繼承
PhysicalAddress

範例

下列程式碼範例會顯示本機電腦上所有介面的實體位址。The following code example displays the physical addresses of all interfaces on the local computer.

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

   Console::WriteLine( L"  Number of interfaces .................... : {0}", (nics->Length).ToString() );
   IEnumerator^ myEnum1 = nics->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      NetworkInterface^ adapter = safe_cast<NetworkInterface^>(myEnum1->Current);
      IPInterfaceProperties^ properties = adapter->GetIPProperties();
      Console::WriteLine();
      Console::WriteLine( adapter->Description );
      Console::WriteLine( String::Empty->PadLeft( adapter->Description->Length, '=' ) );
      Console::WriteLine( L"  Interface type .......................... : {0}", adapter->NetworkInterfaceType );
      Console::Write( L"  Physical address ........................ : " );
      PhysicalAddress^ address = adapter->GetPhysicalAddress();
      array<Byte>^bytes = address->GetAddressBytes();
      for ( int i = 0; i < bytes->Length; i++ )
      {
         
         // Display the physical address in hexadecimal.
         Console::Write( L"{0}", bytes[ i ].ToString( L"X2" ) );
         
         // Insert a hyphen after each byte, unless we are at the end of the 
         // address.
         if ( i != bytes->Length - 1 )
         {
            Console::Write( L"-" );
         }

      }
      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(); //  .GetIPInterfaceProperties();
        Console.WriteLine();
        Console.WriteLine(adapter.Description);
        Console.WriteLine(String.Empty.PadLeft(adapter.Description.Length,'='));
        Console.WriteLine("  Interface type .......................... : {0}", adapter.NetworkInterfaceType);
        Console.Write("  Physical address ........................ : ");
        PhysicalAddress address = adapter.GetPhysicalAddress();
        byte[] bytes = address.GetAddressBytes();
        for(int i = 0; i< bytes.Length; i++)
        {
            // Display the physical address in hexadecimal.
            Console.Write("{0}", bytes[i].ToString("X2"));
            // Insert a hyphen after each byte, unless we are at the end of the 
            // address.
            if (i != bytes.Length -1)
            {
                 Console.Write("-");
            }
        }
        Console.WriteLine();
    }
}

備註

MAC 位址或實體位址是一種硬體位址,可唯一識別網路上的每個節點,例如電腦或印表機。The MAC address, or physical address, is a hardware address that uniquely identifies each node, such as a computer or printer, on a network.

這個類別的實例是由 NetworkInterface.GetPhysicalAddress 方法所傳回。Instances of this class are returned by the NetworkInterface.GetPhysicalAddress method.

建構函式

PhysicalAddress(Byte[])

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

欄位

None

傳回新的 PhysicalAddress 執行個體,其中含有長度為零的位址。Returns a new PhysicalAddress instance with a zero length address. 此欄位為唯讀。This field is read-only.

方法

Equals(Object)

比較兩個 PhysicalAddress 執行個體。Compares two PhysicalAddress instances.

GetAddressBytes()

傳回目前執行個體的位址。Returns the address of the current instance.

GetHashCode()

傳回實體位址的雜湊值 (Hash Value)。Returns the hash value of a physical address.

GetType()

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

(繼承來源 Object)
MemberwiseClone()

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

(繼承來源 Object)
Parse(String)

剖析指定的 String,並將其內容儲存為這個方法所傳回的 PhysicalAddress 之位址位元組。Parses the specified String and stores its contents as the address bytes of the PhysicalAddress returned by this method.

ToString()

傳回這個執行個體的位址之 String 表示。Returns the String representation of the address of this instance.

適用於