请求的名称有效,但是找不到请求的类型的数据。

吞风吻雨 2013-08-07 10:31:01
using System.Net;
using System;
using System.Management;
using System.Runtime.InteropServices;

public class getIP
{
[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);

//获取本机的IP
public string getLocalIP()
{
string strHostName = Dns.GetHostName(); //得到本机的主机名
IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本机IP
string strAddr = ipEntry.AddressList[0].ToString();
return (strAddr);
}
//获取本机的MAC
public string getLocalMac()
{
string mac = null;
ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
ManagementObjectCollection queryCollection = query.Get();
foreach (ManagementObject mo in queryCollection)
{
if (mo["IPEnabled"].ToString() == "True")
mac = mo["MacAddress"].ToString();
}
return (mac);
}

//获取远程主机IP
public string[] getRemoteIP(string RemoteHostName)
{
IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);
IPAddress[] IpAddr = ipEntry.AddressList;
string[] strAddr = new string[IpAddr.Length];
for (int i = 0; i < IpAddr.Length; i++)
{
strAddr[i] = IpAddr[i].ToString();
}
return (strAddr);
}
//获取远程主机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();
}


public static void Main(string[] args)
{
getIP gi = new getIP();
Console.WriteLine("本地网卡信息:");
Console.WriteLine(gi.getLocalIP() + " - " + gi.getLocalMac());

Console.WriteLine("\n\r远程网卡信息:");
string[] temp = gi.getRemoteIP("YHR-PC");
for (int i = 0; i < temp.Length; i++)
{
Console.WriteLine(temp[i]);
}
Console.WriteLine(gi.getRemoteMac("192.168.22.12", "192.168.22.11"));
}
}

目的主机支持ARP。另外请问一下目的主机名不能用之后修改过的计算机名么?
本地网卡信息正常输出,远程网卡信息空白。
...全文
4064 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qydvip 2013-08-07
  • 打赏
  • 举报
回复
本地连接就是你电脑使用的DNS不能正常解析域名导致的 添加备用DNS为8.8.8.8 添加备用DNS开始 --》运行--》cmdnetsh interface ip add dns name="本地连接" addr=8.8.8.8 index=2ipconfig /flushdns 域名不能访问(我不行 他可以)的时候也可以这样处理下 我也是在网上找的 希望可以帮到你

110,566

社区成员

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

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

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