经测试下面方法获取Mac跨平台
protected override void OnLoad(EventArgs e) { Response.Write(string.Join("<br/>", GetMacString())); Response.Write("<br/>"); base.OnLoad(e); } public NetworkInterface[] NetCardInfo() { return NetworkInterface.GetAllNetworkInterfaces(); } public string[] GetMacString() { string strMac = ""; NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { if (ni.OperationalStatus == OperationalStatus.Up) { strMac += ni.GetPhysicalAddress().ToString() + "|"; } } return strMac.Split(‘|‘); }
相关文章:
C# C/S程序获取MAC地址三种方法 http://www.cnblogs.com/yzb305070/archive/2011/04/03/2004364.html
附:
/// <summary> /// 获取当前请求的服务器IP,以及相应的MAC地址 /// </summary> /// <returns>[当前请求的服务器IP,MAC地址]</returns> public string[] GetRequestIPMAC() { string[] result = new string[2]; string serverName = Page.Request.ServerVariables["SERVER_NAME"]; string requestServerIP = Page.Request.ServerVariables["LOCAL_ADDR"]; if (serverName == "localhost") { result[0] = "localhost"; } else { result[0] = requestServerIP.ToLower(); ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { try { if ((bool)mo["IPEnabled"] == true) { string[] IPAddresses = (string[])mo["IPAddress"]; if (IPAddresses.Length > 0) { for (int i = 0; i < IPAddresses.Length; i++) { if (IPAddresses[i] == requestServerIP) { result[1] = mo["MacAddress"].ToString().ToLower(); break; } } } if (!String.IsNullOrEmpty(result[1])) { break; } } } finally { mo.Dispose(); } } } return result; }
时间: 2024-11-03 05:27:57