using System.Runtime.InteropServices;
using System.Diagnostics;
#region 判断网络连接是否连接
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
///
/// 当前网络连接状态
///
/// 当为true时,网络正常;当为false时,网络断开
private bool IsConnected()
{
int I = 0;
bool state = InternetGetConnectedState(out I, 0);
return state;
}
#endregion
#region 判断与某服务器是否连接
///
/// 测试与指定服务器是否正常连接
/// ///
///
private static string CmdPing(string strIp)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string pingrst;
p.Start();
p.StandardInput.WriteLine("ping -n 1 " + strIp);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1)
pingrst = "连接";
else if (strRst.IndexOf("Destination host unreachable.") != -1)
pingrst = "无法到达目的主机";
else if (strRst.IndexOf("Request timed out.") != -1)
pingrst = "超时";
else if (strRst.IndexOf("Unknown host") != -1)
pingrst = "无法解析主机";
else
pingrst = strRst;
p.Close();
return pingrst;
}
#endregion
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-683601/
,如需转载,请注明出处,否则将追究法律责任。