代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Management;
namespace HELLO_WORD_ASP.NET_MVC4.Models
{
public class MacAddress
{
/// <summary>
/// 获取网卡地址信息
/// </summary>
/// <returns></returns>
public string GetNetCardMacAddress()
{
try
{
string macAddress = "";
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;
return macAddress;
}
catch
{
return "unknow";
}
}
}
}
注意事项:ManagementClass 代码粘贴出错,这时候将System.Management 添加引用..