標籤:
擷取網卡MAC第一種
不能判斷當前使用的是那張網卡
1 /// <summary> 2 /// 擷取網卡ID代碼 3 /// </summary> 4 /// <returns></returns> 5 public static string GetNetworkAdpaterID() 6 { 7 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); 8 return nics[0].GetPhysicalAddress().ToString(); 9 10 }
擷取網卡MAC第二種
可以判斷出來當前使用的是哪張網卡 但是要引入
using System.Management;
/// <summary> /// 擷取網卡ID代碼 /// </summary> /// <returns></returns> public static string GetNetworkAdpaterID() { try { string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) if ((bool)mo["IPEnabled"] == true) { mac += mo["MacAddress"].ToString(); break; } string[] macs = mac.Split(‘:‘); mac = ""; foreach (string m in macs) { mac += m; } moc = null; mc = null; return mac.Trim(); } catch (Exception e) { return "uMnIk"; } }
C# 擷取網卡MAC的兩種方式