如果需要获取另外一台机器的Mac地址,可以使用SendARP方法将IP地址解析成MAC地址。网上可以找到现成资料,但大部分只确保返回一个Int64类型的数据,但在将Int64转换为MAC字符串时却少有资料。我对网上的程序重新改写了一番,在这里给出一个完整的例子:

using System;
 using System.Text;
 using System.Runtime.InteropServices;class RemoteMac
 {
    [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);    [STAThread]
    static void Main(string[] args)
    {
       Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));
    }   public static string GetRemoteMAC(string localIP, string remoteIP)
    {
       Int32 ldest= inet_addr(remoteIP); //目的地的ip
       Int32 lhost= inet_addr(localIP);  //本地服务器的ip      string MacStr = "Unknown";
       Int64 macinfo = new Int64(); 
       Int32 len = 6; 
       
       try
       {
          SendARP(ldest,0, ref macinfo, ref len); 
          byte[] b = BitConverter.GetBytes(macinfo); 
          MacStr = ByteArrayToMacString(b); 
       }
       catch
       {
       }      return MacStr;
    }   static public string ByteArrayToMacString(byte[] buff) 
    { 
       StringBuilder sb = new StringBuilder();
       for(int i = 0; i < 6; i++) 
       { 
          sb.Append(String.Format("{0:X2}",buff[i]));
          sb.Append('-');
       }
       sb.Remove(17,1);      return sb.ToString(); 
    } 
 }


实际使用时,请将命令

Console.WriteLine(GetRemoteMAC("***.***.***.***","***.***.***.***"));

中的“***.***.***.***”分别替换成自己机器的IP地址以及希望获取其MAC地址的那台机器的IP地址。