需要通过ActiveX获取。
IE下的代码如下(代码来源):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Getting MAC Address From Javascript(IE Only)</title>
<script language="javascript">
function showMacAddress(){
var obj =new ActiveXObject("WbemScripting.SWbemLocator");
var s = obj.ConnectServer(".");
var properties = s.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e =new Enumerator (properties);
var output;
output='<table border="0" cellPadding="5px" cellSpacing="1px" bgColor="#CCCCCC">';
output=output +'<tr bgColor="#EAEAEA"><td>Caption</td><td>MACAddress</td></tr>';
while(!e.atEnd())
{
e.moveNext();
var p = e.item ();
if(!p) continue;
output=output +'<tr bgColor="#FFFFFF">';
output=output +'<td>'+ p.Caption; +'</td>';
output=output +'<td>'+ p.MACAddress +'</td>';
output=output +'</tr>';
}
output=output +'</table>';
document.getElementById("box").innerHTML=output;
}
</script>
</head>
<body>
<input type="button" value="Show MAC Address" onclick="showMacAddress()"/>
<div id="box">
</div>
</body>
</html>
FireFox中需要安装插件MediaWrap,让FireFox支持ActiveX。(参考自How to get MAC Address in Firefox using Javascript? )
mark!楼上的代码在IE中可以。
用.net实现
代码如下:
public static string GetIP()
{
string HostName = Dns.GetHostName(); //get Hostname
IPHostEntry IpEntry = Dns.GetHostEntry(HostName); //get IP
string strIPAddr = IpEntry.AddressList[0].ToString();
return (strIPAddr);
}
/// <summary>
/// get MacAddress
/// </summary>
/// <returns></returns>
public static string GetMac()
{
string MacAddress = String.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
MacAddress = mo["MacAddress"].ToString();
break;
}
}
moc = null;
mc = null;
MacAddress = MacAddress.Replace(":", "");
return MacAddress;
}