1 string GetMac()
{
string mac = "";
mac = GetMacAddressBySendARP();
return mac;
}
[DllImport("Iphlpapi.dll")]
static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
/// <summary>
/// SendArp获取MAC地址
/// </summary>
/// <returns></returns>
public string GetMacAddressBySendARP()
{
StringBuilder strReturn = new StringBuilder();
try
{
System.Net.IPHostEntry Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(Dns.GetHostName());
System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
Int32 remote = (int)TempAd[].Address;
Int64 macinfo = new Int64();
Int32 length = ;
SendARP(remote, , ref macinfo, ref length);
string temp = System.Convert.ToString(macinfo, ).PadLeft(, '').ToUpper();
int x = ;
for (int i = ; i < ; i++)
{
if (i == ) { strReturn.Append(temp.Substring(x - , )); }
else { strReturn.Append(temp.Substring(x - , ) + ":"); }
x -= ;
}
return strReturn.ToString();
}
catch
{
return "";
}
}
以上代码可直接调用GetMac()函数获取电脑的Mac地址