如何获得指定ip的 MAC 地址

色郎中 2007-02-13 11:17:53
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
有啥更好的办法么?
谢谢
...全文
702 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxiqi 2008-04-16
  • 打赏
  • 举报
回复
up
honkerhero 2007-02-13
  • 打赏
  • 举报
回复
你查一下命令行执行

执行 ipconfig /all

分析返回字符串
ycqing 2007-02-13
  • 打赏
  • 举报
回复
xuedao
copico 2007-02-13
  • 打赏
  • 举报
回复
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;
}
}
}
jxf654 2007-02-13
  • 打赏
  • 举报
回复
up
jinanjiang 2007-02-13
  • 打赏
  • 举报
回复
用WMI
使用时首先添加System.Management.dll,然后引用
using System.Management;
using System.Threading;
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address"+mo["MacAddress"].ToString()+"<br>");
mo.Dispose();
}
}
心晴Sunheart 2007-02-13
  • 打赏
  • 举报
回复
用C#编写获取远程IP,MAC的方法

如果要想获得远程的地址,需要用sendarp这个函数来实现。具体的代码如下:
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);

Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的ip

try
{
Byte[] macinfo=new Byte[6];
Int32 length=6;

IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);

Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);


}
catch(Exception err)
{
Console.WriteLine(err);
}
心晴Sunheart 2007-02-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/sunheartlee/archive/2007/02/13/1509204.aspx
jason_mf 2007-02-13
  • 打赏
  • 举报
回复
xue
色郎中 2007-02-13
  • 打赏
  • 举报
回复
return macinfo;


这个想得到的是 字节 数组 该如何?

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧