// Method invoked by the update process when a download has completed. private void DownloadComplete(object sender, AsyncCompletedEventArgs e) { if (e.Error != null) { mnuNewVersion.Enabled = true; MessageBox.Show(this, string.Format(LocalizationStrings.NotificationThread_FailedToDownload, e.Error.Message), $"{Core.APPLICATION_TITLE}", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { VersionChecker.VersionState downloadedVersionInfo = e.UserState as VersionChecker.VersionState; if (downloadedVersionInfo != null) { ResetVersionMenuClickHandlers(); stripUpdateProgress.Text = LocalizationStrings.NotificationThread_Status_ReadyToInstall; mnuNewVersion.Text = string.Format(LocalizationStrings.NotificationThread_TrayMenu_InstallNewVersion, downloadedVersionInfo.Version); InstallUpdate(null, null); // Code below deprecated in favour of kicking off the install as soon as the download is done. //mnuNewVersion.Enabled = true; //mnuNewVersion.Click += InstallUpdate; //stripNewVersion.Click += InstallUpdate; //stripUpdateProgress.Click += InstallUpdate; //stripUpdateProgress.MouseEnter += Common_MouseEnter; //stripUpdateProgress.MouseLeave += Common_MouseLeave; } } }
// Method used to check the website for any indication that there is a new version available private async Task CheckForNewVersion() { // Aynchronously check for a new version VersionChecker.VersionState result = await Task.Run(() => VersionChecker.CheckVersion(Core.UpdateUrl)).ConfigureAwait(false); // If the check shows there's a new version available if (result.IsNewVersion) { // Pass this result back to the inherited form (to show this in the context menu) base.HasNewVersion(result); } }
// Method for handling whether the Version Checker has found an update, enabling Ui elements accordingly public void HasNewVersion(VersionChecker.VersionState versionDetail) { this.BeginInvoke(new MethodInvoker(() => { stripNewVersion.Tag = versionDetail; stripNewVersion.Visible = true; mnuNewVersion.Text = string.Format(LocalizationStrings.NotificationThread_TrayMenu_DownloadNewVersion, versionDetail.Version); mnuNewVersion.Visible = true; mnuNewVersionSeparator.Visible = true; ResetVersionMenuClickHandlers(); mnuNewVersion.Click += DownloadNewVersion; stripNewVersion.Click += DownloadNewVersion; NotificationHelper.ShowNotification(this, Core.APPLICATION_TITLE, string.Format(LocalizationStrings.PopupNotifications_NewVersionAvailable, versionDetail.Version)); })); }
// Method invoked to install a downloaded update private void InstallUpdate(object sender, EventArgs e) { VersionChecker.VersionState updateInfo = stripNewVersion.Tag as VersionChecker.VersionState; if (updateInfo != null) { string downloadOSFilename = new Uri(updateInfo.Url).PathAndQuery.Replace('/', Path.DirectorySeparatorChar); FileInfo downloadFileInfo = new FileInfo(downloadOSFilename); FileInfo downloadedFile = new FileInfo($"{Core.UserDownloadsPath}{downloadFileInfo.Name}"); string extractionPath = downloadedFile.FullName.Substring(0, downloadedFile.FullName.IndexOf(downloadedFile.Extension)); try { if (downloadedFile.Exists) { using (ZipInputStream s = new ZipInputStream(File.OpenRead(downloadedFile.FullName))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = extractionPath; string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName.Length > 0) { if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(string.Format("{0}\\{1}", directoryName, theEntry.Name))) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } } if (File.Exists(extractionPath + "\\setup.exe")) { ProcessHelper.LaunchProcess($"{extractionPath}\\setup.exe"); ExitApplication(); } } } catch (Exception) { throw; } } }