2016-04-17 59 views
1

獲取MAC地址代碼:C#如何與結腸癌

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

輸出:

0800275283A8

如何隱蔽到:

08:00 :27:52:83:A8

+0

不,不解決我的問題 – abdo

+0

它是怎麼不能解決您的問題? – JanR

+0

'nic'在當前上下文中不存在 – abdo

回答

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