1 /// <summary>
2 /// 是否能 Ping 通指定的主机
3 /// </summary>
4 /// <param name="ip">ip 地址或主机名或域名</param>
5 /// <returns>true 通,false 不通</returns>
6 public bool Ping(string ip)
7 {
8 System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
9 System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
10 options.DontFragment = true;
11 string data = "Test Data!";
12 byte[] buffer = Encoding.ASCII.GetBytes(data);
13 int timeout = 1000; // Timeout 时间,单位:毫秒
14 System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
15 if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
16 return true;
17 else
18 return false;
19 }