如何获得指定ip的 MAC 地址
public static string GetNetCardAddress2(string strIp)
{
string mac = " ";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "nbtstat ";
process.StartInfo.Arguments = "-a "+strIp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
int length = output.IndexOf( "MAC Address = ");
if(length> 0)
{
mac = output.Substring(length+14, 17);
}
process.WaitForExit();
return mac.Replace( "- ", " ").Trim();
}
这种方法 只能获得本机器的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;
}
}
}
------解决方案--------------------xuedao
------解决方案--------------------你查一下命令行执行
执行 ipconfig /all
分析返回字符串
------解决方案--------------------xue
------解决方案--------------------http://blog.csdn.net/sunheartlee/archive/2007/02/13/1509204.aspx
------解决方案--------------------用C#编写获取远程IP,MAC的方法
如果要想获得远程的地址,需要用sendarp这个函数来实现。具体的代码如下: