判断网络是否连接,可以通过ping固定ip的方式实现,下面是实现代码其中checkNet()方法通过ping腾讯ip地址,判断网络是否连接,成功返回true,否则返回false。
public partial class Form1 : Form
{
   if (checkNet())
   {
       MessageBox.Show("网络正常!");
   }
   else
   {
      MessageBox.Show("网络异常,请检查网络!");
   }
}
private bool checkNet() {
  try {
        System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
        System.Net.NetworkInformation.PingReply pr;
        pr = ping.Send("125.39.240.113");
        if (pr.Status == System.Net.NetworkInformation.IPStatus.Success)
        {
           return true;
        }
        else
        {
          return false;
        }
     }
     catch {
         return false;
     }
}

Comments | NOTHING