/// <summary> /// Event called when an update file has completed its download /// </summary> private static void Wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { // Close task form, and unregister for updates TaskForm.Cancelled -= TaskForm_Cancelled; TaskForm.CloseForm(); IsDownloading = false; // Dispose webclient Web.Dispose(); // If we cancelled, stop here if (e.Cancelled) { // Delete junk files if (File.Exists(UpdateFileLocation)) { File.Delete(UpdateFileLocation); } return; } // Try to start the isntaller try { // Extract setup.exe string exFile = Path.Combine(Paths.DocumentsFolder, "setup.exe"); using (ZipArchive file = ZipFile.Open(UpdateFileLocation, ZipArchiveMode.Read)) { ZipArchiveEntry setupFile = file.Entries.FirstOrDefault(x => x.FullName == "setup.exe"); if (setupFile != null) { // Extract and start the new update installer setupFile.ExtractToFile(exFile, true); Process installer = Process.Start(exFile); installer.WaitForInputIdle(); } else { MessageBox.Show( "The Setup.exe file appears to be missing from the update archive! You will need to manually apply the update.", "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); } } } catch (Exception Ex) { MessageBox.Show( "An Occured while trying to install the new update. You will need to manually apply the update." + Environment.NewLine.Repeat(1) + "Error Message: " + Ex.Message, "Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); ExceptionHandler.GenerateExceptionLog(Ex); } // Exit the application Application.Exit(); }
/// <summary> /// Downloads the new update from Github Async. /// </summary> public static bool DownloadUpdateAsync() { // Returns if there is no update if (!UpdateAvailable) { return(false); } // Simulate some headers, Github throws a fit otherwise Web = new WebClient(); Web.Headers["User-Agent"] = "BF2Statistics Control Center v" + Program.Version; Web.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; Web.Headers["Accept-Language"] = "en-US,en;q=0.8"; Web.Proxy = null; // Disable proxy because this can cause slowdown on some machines // Github file location string Download = "https://github.com/BF2Statistics/ControlCenter/releases/download/{0}/BF2Statistics_ControlCenter_{0}.zip"; Uri FileLocation = new Uri(String.Format(Download, NewVersion)); // Path to the Downloaded file UpdateFileLocation = Path.Combine(Paths.DocumentsFolder, String.Format("BF2Statistics_ControlCenter_{0}.zip", NewVersion)); // Show Task Form IsDownloading = true; TaskForm.Cancelled += TaskForm_Cancelled; TaskForm.Show(MainForm.Instance, "Downloading Update", "Downloading Update... Please Standby", true); TaskForm.Progress.Report(new TaskProgressUpdate("Preparing the download...")); try { // Download the new version Zip file Web.DownloadProgressChanged += Wc_DownloadProgressChanged; Web.DownloadFileCompleted += Wc_DownloadFileCompleted; Web.DownloadFileAsync(FileLocation, UpdateFileLocation); } catch (Exception ex) { // Close that task form if its open! if (TaskForm.IsOpen) { TaskForm.CloseForm(); } // Create Exception Log Program.ErrorLog.Write("WARNING: Unable to Download new update archive :: Generating Exception Log"); ExceptionHandler.GenerateExceptionLog(ex); // Alert User MessageBox.Show( "Failed to download update archive! Reason: " + ex.Message + Environment.NewLine.Repeat(1) + "An exception log has been generated and created inside the My Documents/BF2Statistics folder.", "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error ); return(false); } // Tell the mainform that we have this handled return(true); }