From a2fffe2dc140e101487983b5f3080c77eb72ef9e Mon Sep 17 00:00:00 2001 From: komaruchan Date: Thu, 4 Jun 2015 00:07:48 +0800 Subject: [PATCH] detect whether online before checking update. --- mp4box/MainForm.cs | 17 ++++++++++++----- mp4box/Util.cs | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/mp4box/MainForm.cs b/mp4box/MainForm.cs index fa557cf..c61b69f 100644 --- a/mp4box/MainForm.cs +++ b/mp4box/MainForm.cs @@ -883,7 +883,7 @@ namespace mp4box else languageComboBox.SelectedIndex = int.Parse(ConfigurationManager.AppSettings["LanguageIndex"]); - if (CheckUpdateCheckBox.Checked) + if (CheckUpdateCheckBox.Checked && Util.IsConnectInternet()) { DateTime d; bool f; @@ -3765,10 +3765,17 @@ namespace mp4box private void CheckUpdateButton_Click(object sender, EventArgs e) { - WebRequest request = WebRequest.Create("http://mtbftest.sinaapp.com/version.php"); - request.Credentials = CredentialCache.DefaultCredentials; - // Get the response. - request.BeginGetResponse(new AsyncCallback(OnResponse), request); + if (Util.IsConnectInternet()) + { + WebRequest request = WebRequest.Create("http://mtbftest.sinaapp.com/version.php"); + request.Credentials = CredentialCache.DefaultCredentials; + // Get the response. + request.BeginGetResponse(new AsyncCallback(OnResponse), request); + } + else + { + ShowErrorMessage("这台电脑似乎没有联网呢~"); + } } protected void OnResponse(IAsyncResult ar) diff --git a/mp4box/Util.cs b/mp4box/Util.cs index 80a596c..6d5ced3 100644 --- a/mp4box/Util.cs +++ b/mp4box/Util.cs @@ -26,6 +26,8 @@ using System.Diagnostics; using System.Windows.Forms; using System.IO; using System.Threading; +using System.Net.NetworkInformation; +using System.Runtime.InteropServices; namespace mp4box { @@ -216,5 +218,18 @@ namespace mp4box return true; } + + + [DllImport("wininet.dll")] + private extern static bool InternetGetConnectedState(int Description, int ReservedValue); + /// + /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败 + /// + /// + public static bool IsConnectInternet() + { + int Description = 0; + return InternetGetConnectedState(Description, 0); + } } } -- 2.11.4.GIT