根据ip地址获得Mac地址的一种方法
Code
public static class MacUtility
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public static string GetRemoteMac(string clientIP)
{
string ip = clientIP;
if (ip == "127.0.0.1")
ip = GetLocalIP()[0];
var ldest = inet_addr(ip);
Int64 macinfo = new Int64();
Int32 len = 6;
try
{
SendARP(ldest, 0, ref macinfo, ref len);
}
catch
{
return "";
}
string originalMACAddress = Convert.ToString(macinfo, 16);
if (originalMACAddress.Length < 12)
{
originalMACAddress = originalMACAddress.PadLeft(12, '0');
}
string macAddress;
if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
{
string mac1, mac2, mac3, mac4, mac5, mac6;
mac1 = originalMACAddress.Substring(10, 2);
mac2 = originalMACAddress.Substring(8, 2);
mac3 = originalMACAddress.Substring(6, 2);
mac4 = originalMACAddress.Substring(4, 2);
mac5 = originalMACAddress.Substring(2, 2);
mac6 = originalMACAddress.Substring(0, 2);
macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
}
else
{
macAddress = "";
}
return macAddress.ToUpper();
}
public static string[] GetLocalIP()
{
string hostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(hostName);
IPAddress[] arr = ipEntry.AddressList;
string[] result = new string[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
result[i] = arr[i].ToString();
}
return result;
}
}
public static class MacUtility
{
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
public static string GetRemoteMac(string clientIP)
{
string ip = clientIP;
if (ip == "127.0.0.1")
ip = GetLocalIP()[0];
var ldest = inet_addr(ip);
Int64 macinfo = new Int64();
Int32 len = 6;
try
{
SendARP(ldest, 0, ref macinfo, ref len);
}
catch
{
return "";
}
string originalMACAddress = Convert.ToString(macinfo, 16);
if (originalMACAddress.Length < 12)
{
originalMACAddress = originalMACAddress.PadLeft(12, '0');
}
string macAddress;
if (originalMACAddress != "0000" && originalMACAddress.Length == 12)
{
string mac1, mac2, mac3, mac4, mac5, mac6;
mac1 = originalMACAddress.Substring(10, 2);
mac2 = originalMACAddress.Substring(8, 2);
mac3 = originalMACAddress.Substring(6, 2);
mac4 = originalMACAddress.Substring(4, 2);
mac5 = originalMACAddress.Substring(2, 2);
mac6 = originalMACAddress.Substring(0, 2);
macAddress = mac1 + "-" + mac2 + "-" + mac3 + "-" + mac4 + "-" + mac5 + "-" + mac6;
}
else
{
macAddress = "";
}
return macAddress.ToUpper();
}
public static string[] GetLocalIP()
{
string hostName = Dns.GetHostName();
IPHostEntry ipEntry = Dns.GetHostByName(hostName);
IPAddress[] arr = ipEntry.AddressList;
string[] result = new string[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
result[i] = arr[i].ToString();
}
return result;
}
}
作者:Lance
出处:http://www.cnblogs.com/nuaalfm/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。