我正在尝试获取在LAN中连接的IP地址列表, 是否可以不使用win32 API库。(Netapi32.dll)。 没有使用非托管win32动态库.target到Windows 7操作系统的任何想法
答案 0 :(得分:2)
如果您收到有使用该IP的计算机的响应,则可以使用System.Net.NetworkInformation.Ping ping子网中的每个IP。如果您没有得到ip可用的响应,或者机器忽略了ping。
已更新,可添加代码并行执行此操作。
public string IPList()
{
string myipsplit = string.Empty;
string localhostname = Dns.GetHostName();
IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
string myip = paddresses.Where( ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
string[] myiparray = myip.Split(new[] { '.' });
for (int j = 1; j < myiparray.Length; j++)
myipsplit += myiparray[j - 1] + ".";
Trace.WriteLine(DateTime.Now);
var results = new string[0x100];
System.Threading.Tasks.Parallel.For(1, 0x100, id =>
{
var pingSender = new Ping();
string ls = myipsplit + id;
PingReply reply = pingSender.Send(ls, 100);
if (reply != null)
if (reply.Status == IPStatus.Success)
results[id] = reply.Address.ToString();
});
Trace.WriteLine(DateTime.Now);
var sb = new StringBuilder();
results.All(x => { sb.AppendFormat("{0} ", x);
return true;
});
return sb.ToString();
}
更新为.Net 2.0
private delegate void MyPing(int id);
public string IPList2()
{
string myipsplit = string.Empty;
string localhostname = Dns.GetHostName();
IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
string myip = paddresses.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().ToString();
string[] myiparray = myip.Split(new[] { '.' });
for (int j = 1; j < myiparray.Length; j++)
myipsplit += myiparray[j - 1] + ".";
Trace.WriteLine(DateTime.Now);
var results = new string[0x100];
MyPing ping =
id =>
{
string ls = myipsplit + id;
var pingSender = new Ping();
PingReply reply = pingSender.Send(ls, 100);
if (reply != null)
if (reply.Status == IPStatus.Success)
results[id] = reply.Address.ToString();
};
var asyncResults = new IAsyncResult[0x100];
for (int i = 1; i < 0x100; i++)
{
asyncResults[i] = ping.BeginInvoke(i, null, null);
}
for (int i = 1; i < 0x100; i++)
{
ping.EndInvoke(asyncResults[i]);
}
Trace.WriteLine(DateTime.Now);
var sb = new StringBuilder();
for (int i = 1; i < 0x100; i++)
{
if (results[i]!=null)
sb.AppendFormat("{0} ", results[i]);
}
return sb.ToString();
}
答案 1 :(得分:0)
您可以使用NetworkInterface
对象获取所有计算机网络接口。
var networkCards = System.Net.NetworkInterface.GetAllNetworkInterfaces();
foreach(var card in networkCards)
Console.WriteLine(card.GetPhysicalAddress());
答案 2 :(得分:0)
public string IPList()
{
var pingSender = new Ping();
string port = string.Empty;
string myipsplit = string.Empty;
string localhostname = Dns.GetHostName();
IPAddress[] paddresses = Dns.GetHostAddresses(localhostname);
string myip = paddresses[0].ToString();
string[] myiparray = myip.Split(new[] {'.'});
for (int j = 1; j < myiparray.Length; j++)
myipsplit += myiparray[j - 1] + ".";
Trace.WriteLine(DateTime.Now);
for (int i = 0; i < 0x100; i++)
{
string ls = myipsplit + i;
PingReply reply = pingSender.Send(ls, 0);
if (reply != null)
if (reply.Status == IPStatus.Success)
port += reply.Address + "+";
}
Trace.WriteLine(DateTime.Now);
return port;
}