C#判断网络状态
代码
/// <summary>
/// 是否能 Ping 通指定的主机
/// </summary>
/// <param name="ip">ip 地址或主机名或域名</param>
/// <returns>true 通,false 不通</returns>
public bool Ping(string ip)
{
int timeout = 1000;
string data = "Test Data!";
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true; byte[] buffer = Encoding.ASCII.GetBytes(data);
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) return true; else return false;
}
/// 是否能 Ping 通指定的主机
/// </summary>
/// <param name="ip">ip 地址或主机名或域名</param>
/// <returns>true 通,false 不通</returns>
public bool Ping(string ip)
{
int timeout = 1000;
string data = "Test Data!";
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true; byte[] buffer = Encoding.ASCII.GetBytes(data);
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success) return true; else return false;
}