62,051
社区成员
发帖
与我相关
我的任务
分享
private string CheckNetWork(string _strUrl)
{
//实例一个Process类,启动一个独立进程
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("telnet -n 1 " + _strIP + " 84564646");
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
//strRst = p.StandardError.ReadToEnd();
//p.WaitForExit();
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 = "网络无效";
p.Close();
return PingRst;
}
[DllImport("wininet")]
private extern static bool InternetGetConnectedState(out int connectionDescription, int reservedValue);
///<summary>
/// 检测本机的网络连接
///</summary>
private void button1_Click(object sender, EventArgs e)
{
//判断是否联网
int i = 0;
if (InternetGetConnectedState(out i, 0))
{
//联网
MessageBox.Show("Thylx提醒您:你的计算机已连接到网络上!");
}
else
{
//断网
MessageBox.Show("Thylx提醒您:本地连接已断开!");
}
}
try
{
System.Net.WebClient wc =new System.Net.WebClient();
string bb=wc.DownloadString("http://www.baidu.com");
}
catch(Exception ex)
{
//没有联网
return "我就不返回0";
}