【问题标题】:Formatting MAC address in C#在 C# 中格式化 MAC 地址
【发布时间】:2011-11-15 02:24:44
【问题描述】:

在我的 C# 应用程序中,我想通过使用 NetworkInterface 类来获取我的 MAC 地址,如下所示:

NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()
{
    mac = nic.GetPhysicalAddress()
}

但此代码返回的 MAC 不带 ':' 或任何其他分隔符。

如何以这种格式检索 MAC: 88:88:88:88:87:88 只使用上面的代码吗?

【问题讨论】:

  • “只使用上面的代码”?如果它没有返回带有任何分隔符的 MAC 地址......你不能只使用它。
  • 我想格式化它。你知道怎么做吗?
  • @gln - 检查我在哪里格式化了 mac 地址

标签: c# string format mac-address


【解决方案1】:

试试

mac = string.Join (":", (from z in nic.GetPhysicalAddress().GetAddressBytes() select z.ToString ("X2")).ToArray());

【讨论】:

  • 在 .NET 4 中它可以更短mac = string.Join (":", nic.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2")));
  • 或:mac = string.Join (":", Array.ConvertAll(nic.GetPhysicalAddress().GetAddressBytes(), b => b.ToString("X2")));
【解决方案2】:

该命令的帮助显示了一种方式:

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

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

【讨论】:

  • 这可以做得更短:BitConverter.ToString(byte[]) 正是这种格式。
  • @eFloh:将其发布为答案。
  • @BoltClock:这已经在下面链接的帖子中,仅适用于该帖子,因为它不会产生请求的输出。请参阅this answer to Getting MAC Address C# 以获得实际响应。
【解决方案3】:

使用comment by eFloh 来使用BitConverter 我能够执行以下操作(假设mac 被预定义为字符串)。

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
    mac = BitConverter.ToString(nic.GetPhysicalAddress().GetAddressBytes()).Replace('-', ':');

    //Do whatever else necessary with each mac...
}

【讨论】:

    【解决方案4】:

    试试这样的:

    // Insert Colons on MAC
    string MACwithColons = "";
    for (int i = 0; i < MAC.Length; i++) {
      MACwithColons = MACwithColons + MAC.Substring(i, 2) + ":";
      i++;
    }
    MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1); // Remove the last colon
    

    【讨论】:

      【解决方案5】:

      你想在哪里展示,你必须这样做:

      txtMac.text=getFormatMac(GetMacAddress());
      public string GetMacAddress()
      
      {
          NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
          String sMacAddress = string.Empty;
          foreach (NetworkInterface adapter in nics)
          {
               if (sMacAddress == String.Empty)// solo retorna la mac de la primera tarjeta
               {
                    IPInterfaceProperties properties = adapter.GetIPProperties();
                    sMacAddress = adapter.GetPhysicalAddress().ToString();
                }
          }
          return sMacAddress;
      }
      
      public string getFormatMac(string sMacAddress)
      {
          string MACwithColons = "";
          for (int i = 0; i < macName.Length; i++)
          {
              MACwithColons = MACwithColons + macName.Substring(i, 2) + ":";
              i++;
          }
          MACwithColons = MACwithColons.Substring(0, MACwithColons.Length - 1);
      
          return MACwithColons;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-11-15
        • 1970-01-01
        • 2023-03-31
        • 2020-06-10
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多