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

返回物理地址的哈希值。Returns the hash value of a physical address.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Parse(ReadOnlySpan<Char>)

分析指定的范围,并将其内容存储为由此方法返回的 PhysicalAddress 的地址字节。Parses the specified span and stores its contents as the address bytes of the PhysicalAddress returned by this method.

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.

TryParse(ReadOnlySpan<Char>, PhysicalAddress)

尝试将硬件地址的范围表示形式转换为 PhysicalAddress 实例。Tries to convert the span representation of a hardware address to a PhysicalAddress instance. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded.

TryParse(String, PhysicalAddress)

尝试将硬件地址的字符串表示形式转换为 PhysicalAddress 实例。Tries to convert the string representation of a hardware address to a PhysicalAddress instance. 一个指示转换是否成功的返回值。A return value indicates whether the conversion succeeded.

适用于