PhysicalAddress PhysicalAddress PhysicalAddress PhysicalAddress Class

定義

提供網路介面 (介面卡) 的媒體存取控制 (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
繼承
PhysicalAddressPhysicalAddressPhysicalAddressPhysicalAddress

範例

下列程式碼範例會顯示本機電腦上的所有介面實體位址。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(Byte[]) PhysicalAddress(Byte[]) PhysicalAddress(Byte[])

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

欄位

None None None None

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

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

GetAddressBytes() GetAddressBytes() GetAddressBytes() GetAddressBytes()

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

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

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

GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

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

(Inherited from Object)
Parse(String) Parse(String) Parse(String) Parse(String)

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

ToString() ToString() ToString() ToString()

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

適用於