C#获取网络状态
/// <summary>
/// 获取网络状态
/// </summary>
/// <param name="ip">目标IP地址</param>
/// <returns></returns>
public bool GetNewtWorkStatus(string ip)
{
if (ip == "." || ip == "127.0.0.1" || ip == "localhost")
return NetworkInterface.GetIsNetworkAvailable();
int splitPos = ip.IndexOf('\\');
if (splitPos > 0)
{
ip = ip.Substring(0, splitPos);
}
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "lzltest";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = this.delaytime > 0 ? delaytime : 500;
PingReply reply = pingSender.Send(ip, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
return true;
return false;
}