C#如何用冒号获取mac地址

时间:2016-04-17 23:20:20

标签: c# networking

代码:

var macAddr =
      (from nic in NetworkInterface.GetAllNetworkInterfaces()
       where nic.OperationalStatus == OperationalStatus.Up
       select nic.GetPhysicalAddress().ToString()).FirstOrDefault();

输出:

  

0800275283A8

如何隐蔽:

  

08:00:27:52:83:A8

1 个答案:

答案 0 :(得分:1)

试试这个:

 var macAddr =
              (from nic in NetworkInterface.GetAllNetworkInterfaces()
               where nic.OperationalStatus == OperationalStatus.Up
               select nic.GetPhysicalAddress().GetAddressBytes()).FirstOrDefault();

 string formattedMacAddr = string.Join (":", (from z in macAddr select z.ToString ("X2")).ToArray());
 Console.WriteLine(formattedMacAddr);

//outputs this format: 08:00:27:52:83:A8