正好我有现成的代码 public static string getMacByIPAddress(string ip) { return BitConverter.ToString(getMacByIPAddress(System.Net.IPAddress.Parse(ip))); }public static byte[] getMacByIPAddress(System.Net.IPAddress ip) { Int64 macinfo = new Int64(); Int32 len = 6; int res = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, ref macinfo, ref len); if (res != 0) { throw new Exception("IP不存在"); } byte[] bs = new byte[6];// 这个就是需要的 MAC 地址 Array.Copy(BitConverter.GetBytes(macinfo), bs, 6); return bs; }[System.Runtime.InteropServices.DllImport("IpHlpApi.dll")] private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);[System.Runtime.InteropServices.DllImport("ws2_32.dll")] private static extern Int32 inet_addr(String ip);
是获取远程服务器MAC,不是本机MAC
参考下 .net下的wmi 也将是 system.management
4楼就是的,详细的你可以了解arp协议
using[i] System.Net; using System.Management;
string Ip = "192.168.1.20",Mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) Mac = mo["MacAddress"].ToString(); mo.Dispose(); }
服务器上面安装有SQL2005
使用SQL语句获得服务器MAC,也行。
http://blog.csdn.net/honkerhero/article/details/1473586
public static string getMacByIPAddress(string ip)
{
return BitConverter.ToString(getMacByIPAddress(System.Net.IPAddress.Parse(ip)));
}public static byte[] getMacByIPAddress(System.Net.IPAddress ip)
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(BitConverter.ToInt32(ip.GetAddressBytes(), 0), 0, ref macinfo, ref len);
if (res != 0)
{
throw new Exception("IP不存在");
}
byte[] bs = new byte[6];// 这个就是需要的 MAC 地址
Array.Copy(BitConverter.GetBytes(macinfo), bs, 6);
return bs;
}[System.Runtime.InteropServices.DllImport("IpHlpApi.dll")]
private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);[System.Runtime.InteropServices.DllImport("ws2_32.dll")]
private static extern Int32 inet_addr(String ip);
4楼就是的,详细的你可以了解arp协议
using System.Management;
string Ip = "192.168.1.20",Mac = "";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
Mac = mo["MacAddress"].ToString();
mo.Dispose();
}
请问,怎么在winform text中显示出来获得得服务器的MAC呢?
请详细写代码。
我在这台机器上运行这个软件,无法获取得到服务器的Mac
所以,请教...
//获取远程主机MAC
public string getRemoteMac(string localIP, string remoteIP)
{
Int32 ldest= inet_addr(remoteIP); //目的ip
Int32 lhost= inet_addr(localIP); //本地ip try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return Convert.ToString(macinfo,16);
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0.ToString();
}
更多内容参考这个博文:http://www.cnblogs.com/deckard/archive/2009/04/07/1430807.html