WinForm获取客户端MAC地址
//获取MAC public string GetMacString() { string resMac = string.Empty; try { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc2 = mc.GetInstances(); foreach (ManagementObject mo in moc2) { if ((bool)mo["IPEnabled"] == true) { resMac = mo["MacAddress"].ToString(); mo.Dispose(); resMac = resMac.Replace(":", "-");//将“:”替换为“-” return resMac; } } } catch (Exception) { resMac = "00-00-00-00-00-00"; } return resMac.ToLower(); } #endregion
ManagementClass 需要引用 右键项目添加引用system.ManagementClass