C# winform 檢測網絡是否暢通 檢測

   /// <summary>
        /// 聯網檢測20200529
        /// </summary>
      public void 網絡() {

            Ping pingSender = new Ping();
            PingReply reply = null;
            try
            {
                reply = pingSender.Send("www.baidu.com", 1000);
            }
            catch (Exception)
            {
            }
            finally
            {
                if (reply == null || (reply != null && reply.Status != IPStatus.Success))
                {
                    btn_Update.Enabled = false;
                    btn_Add.Enabled = false;
                    labelnetwork.Text = "無法連接該網站,請檢查網絡! 連接網站失敗";
                    MessageBox.Show("無法連接該網站,請檢查網絡!", "連接網站失敗", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else if (reply.Status == IPStatus.Success) {
                    labelnetwork.Text = "網絡連接成功......";
                    btn_Update.Enabled = true;
                    btn_Add.Enabled = true;
                }
                //this.Text = "連接成功";
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章