【发布时间】:2014-04-20 13:34:50
【问题描述】:
我发现这个用于简单 IP 网络扫描仪的 C# 代码,它扫描网络中连接的主机并显示其物理地址和 IP 地址。它工作得很好,但只有通过 WIFI 连接到网络时。通过电线通过网络连接时,它不起作用。
[DllImport("iphlpapi.dll", ExactSpelling = true)]
一开始,iphlpapi.dll 已经被导入。那么,你能解释一下吗?其余代码如下。
// Use Your work Group WinNT://&&&&(Work Group Name)
DirectoryEntry DomainEntry = new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());
DomainEntry.Children.SchemaFilter.Add("computer");
// To Get all the System names And Display with the Ip Address
foreach (DirectoryEntry machine in DomainEntry.Children)
{
string[] Ipaddr = new string[3];
Ipaddr[0] = machine.Name;
System.Net.IPHostEntry Tempaddr = null;
try
{
Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);
}
catch (Exception)
{
MessageBox.Show("Unable to connect with the system :" + machine.Name);
continue;
}
IPAddress[] TempAd = Tempaddr.AddressList;
foreach (IPAddress TempA in TempAd)
{
Ipaddr[1] = TempA.ToString();
byte[] ab = new byte[6];
int len = ab.Length;
// This Function Used to Get The Physical Address
int r = SendARP((int)TempA.Address, 0, ab, ref len);
string mac = BitConverter.ToString(ab, 0, 6);
Ipaddr[2] = mac;
}
ListViewItem TempItem = new ListViewItem(Ipaddr);
this.ListHostIP.Items.Add(TempItem);
}
}
【问题讨论】:
-
在我看来,单独的连接方法不太可能是原因 - 我会关注这是否真的是真的。
-
看看这里 iphlpapi 的全部内容:msdn.microsoft.com/en-us/library/windows/desktop/… oyu 是否得到上述代码的异常?
-
我只是想知道是否可以对此代码进行更改,以便在通过电线连接时显示结果。如果是,我该怎么做?请提出建议。
标签: c# .net networking ip