C# 获取MAC地址
using System;
using System.Runtime.InteropServices;
namespace UtilityControl
{
/// <summary>
/// 关于IP地址的若干操作
/// </summary>
public class IP
{
public IP()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[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);
/// <summary>
/// 根据ip得到网卡mac地址
/// </summary>
/// <param name="ip">给出的ip地址</param>
/// <returns>对应ip的网卡mac地址</returns>
public static Int64 GetMACByIP(string ip)
{
Int32 ldest= inet_addr(ip); //目的地的ip
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
}
}
}
using System.Runtime.InteropServices;
namespace UtilityControl
{
/// <summary>
/// 关于IP地址的若干操作
/// </summary>
public class IP
{
public IP()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[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);
/// <summary>
/// 根据ip得到网卡mac地址
/// </summary>
/// <param name="ip">给出的ip地址</param>
/// <returns>对应ip的网卡mac地址</returns>
public static Int64 GetMACByIP(string ip)
{
Int32 ldest= inet_addr(ip); //目的地的ip
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
}
}
}