C#中通过SendARP读取MAC地址:
using System.Runtime.InteropServices; publicstaticstring GetMacBySendARP(string remoteIP)
{
StringBuilder macAddress =new StringBuilder(); try
{
Int32 remote = inet_addr(remoteIP); Int64 macInfo =new Int64();
Int32 length =;
SendARP(remote, , ref macInfo, ref length); string temp = Convert.ToString(macInfo, ).PadLeft(, '').ToUpper(); int x =;
for (int i =; i <; i++)
{
if (i ==)
{
macAddress.Append(temp.Substring(x -, ));
}
else
{
macAddress.Append(temp.Substring(x -, ) +"-");
}
x -=;
} return macAddress.ToString();
}
catch
{
return macAddress.ToString();
}
} [DllImport("Iphlpapi.dll")]
privatestaticexternint SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
privatestaticextern Int32 inet_addr(string ip);