private void AutoCheckForUpdate() { Version webVersion = null; string webVersionText = ""; try { _web = new WebClient(); _web.Proxy = WebRequest.GetSystemWebProxy(); _web.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; _web.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); while (true) { try { webVersionText = _web.DownloadString("http://www.kitchengeeks.net/Malifaux/LuciusIncidentLogbook/.version"); break; } catch { return; } } webVersion = new Version(webVersionText + ".0"); if (LogWindow != null) LogWindow.WriteLog("[Updater] Local version = v" + Program.VersionString + ", Web version = v" + webVersionText); } catch { return; } if (webVersion <= Program.Version) { _web = null; return; } if (MessageBox.Show("A newer version is available: v" + webVersionText + " Update now?", "Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.No) return; try { StatusForm = new frmDownload(); StatusForm.Show(); StatusForm.SetMessage("Initializing..."); _downloaded = false; _web.DownloadProgressChanged += web_DownloadProgressChanged; _web.DownloadFileCompleted += web_DownloadFileCompleted; _web.DownloadFileAsync( new Uri("http://www.kitchengeeks.net/Malifaux/LuciusIncidentLogbook/LuciusIncidentLogbook.exe"), Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "LuciusIncidentLogbook.Update.exe")); while (!_downloaded) { Thread.Sleep(100); Application.DoEvents(); } StatusForm.SetProgressValue(100); StatusForm.SetProgressMax(100); StatusForm.SetMessage("Download Completed!"); Application.DoEvents(); Thread.Sleep(1000); Process.Start( Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "LuciusIncidentLogbook.Update.exe"), "/update " + Process.GetCurrentProcess().Id.ToString() + " " + Program.VersionString); Application.Exit(); } catch (Exception ex) { MessageBox.Show("An error occurred while downloading the update: " + ex.Message + " You may retry " + "from the Help menu.", "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } finally { StatusForm.Close(); StatusForm = null; _web = null; } }
private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e) { _web = new WebClient(); _web.Proxy = WebRequest.GetSystemWebProxy(); _web.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; _web.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); StatusForm = new frmDownload(); StatusForm.Show(); StatusForm.SetMessage("Checking version number..."); Application.DoEvents(); string webVersionText = ""; while (true) { try { webVersionText = _web.DownloadString("http://www.kitchengeeks.net/Malifaux/LuciusIncidentLogbook/.version"); break; } catch (Exception ex) { if (MessageBox.Show("Unable to check for the latest version: " + ex.Message, "Error During Update Check", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2) == DialogResult.Cancel) { StatusForm.Close(); StatusForm = null; return; } } } var webVersion = new Version(webVersionText + ".0"); if (LogWindow != null) LogWindow.WriteLog("[Updater] Local version = v" + Program.VersionString + ", Web version = v" + webVersionText); if (webVersion <= Program.Version) { StatusForm.Close(); StatusForm = null; MessageBox.Show("You have the latest version!", "Up To Date", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (MessageBox.Show("A newer version is available: v" + webVersionText + " Update now?", "Update Available", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.No) { StatusForm.Close(); StatusForm = null; return; } StatusForm.SetMessage("Initializing..."); _downloaded = false; _web.DownloadProgressChanged += web_DownloadProgressChanged; _web.DownloadFileCompleted += web_DownloadFileCompleted; _web.DownloadFileAsync( new Uri("http://www.kitchengeeks.net/Malifaux/LuciusIncidentLogbook/LuciusIncidentLogbook.exe"), Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "LuciusIncidentLogbook.Update.exe")); while (!_downloaded) { Thread.Sleep(100); Application.DoEvents(); } StatusForm.SetProgressValue(100); StatusForm.SetProgressMax(100); StatusForm.SetMessage("Download Completed!"); Application.DoEvents(); Thread.Sleep(1000); StatusForm.Close(); StatusForm = null; _web = null; Process.Start( Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "LuciusIncidentLogbook.Update.exe"), "/update " + Process.GetCurrentProcess().Id.ToString() + " " + Program.VersionString); Application.Exit(); }