C#获得局域网内所有正在使用的ip
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6 using System.Net.NetworkInformation;
7
8 namespace Ping
9 {
10 class Program
11 {
12 staticvoid Main(string[] args)
13 {
14 for (int i =2; i <255; i++)
15 {
16
17 System.Net.NetworkInformation.Ping myPing;
18 myPing =new System.Net.NetworkInformation.Ping();
19 string pingIP ="192.168.1."+ i.ToString();
20 myPing.SendAsync(pingIP, 1000, null);
21 myPing.PingCompleted +=new PingCompletedEventHandler(_myPing_PingCompleted);
22
23 }
24 Console.ReadLine();
25 }
26 staticvoid _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
27 {
28 if (e.Reply.Status == IPStatus.Success)
29 {
30 Console.WriteLine(e.Reply.Address.ToString() +"|"+ Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName.ToString());
31 }
32 }
33 }
34 }
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6 using System.Net.NetworkInformation;
7
8 namespace Ping
9 {
10 class Program
11 {
12 staticvoid Main(string[] args)
13 {
14 for (int i =2; i <255; i++)
15 {
16
17 System.Net.NetworkInformation.Ping myPing;
18 myPing =new System.Net.NetworkInformation.Ping();
19 string pingIP ="192.168.1."+ i.ToString();
20 myPing.SendAsync(pingIP, 1000, null);
21 myPing.PingCompleted +=new PingCompletedEventHandler(_myPing_PingCompleted);
22
23 }
24 Console.ReadLine();
25 }
26 staticvoid _myPing_PingCompleted(object sender, PingCompletedEventArgs e)
27 {
28 if (e.Reply.Status == IPStatus.Success)
29 {
30 Console.WriteLine(e.Reply.Address.ToString() +"|"+ Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName.ToString());
31 }
32 }
33 }
34 }