private void myWebClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { if ((e.Error != null)) { this.OnHandledException(new HandledExceptionEventArgs(e.Error)); } else { if (e.UserState is Version) { this.OnDownloadUpdate((Version)e.UserState); } else if (e.UserState is DownloadFileMeta) { DownloadFileMeta state = (DownloadFileMeta)e.UserState; if ((state.State == "downloadupdater")) { this.RaiseEventStepChanged("Downloading new version"); using (FileStream fsss = new FileStream(this.UpdaterPath + ".7z", FileMode.Open, FileAccess.Read, FileShare.Read)) if (SharpCompress.Archives.SevenZip.SevenZipArchive.IsSevenZipFile(fsss)) { fsss.Position = 0; using (SharpCompress.Archives.SevenZip.SevenZipArchive archive = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(fsss)) using (SharpCompress.Readers.IReader reader = archive.ExtractAllEntries()) while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { if (reader.Entry.Key.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { using (FileStream fs = File.Create(this.UpdaterPath)) reader.WriteEntryTo(fs); break; } } } } else { this.OnHandledException(new HandledExceptionEventArgs(new FileNotFoundException("Updater not found", this.UpdaterPath))); } try { File.Delete(this.UpdaterPath + ".7z"); } catch { } this.OnProgressBarStateChanged(new Events.ProgressBarStateChangedEventArgs(Forms.MyMainMenu.ProgressBarVisibleState.Percent)); this.myWebClient.DownloadFileAsync(this.UpdateUri, Path.ChangeExtension(Leayal.AppInfo.ApplicationFilename, ".update-" + state.Ver.ToString()) + ".7z", state.Ver); } } } }
protected virtual void OnDownloadUpdate(Version ver) { this._CurrentProgress = -1; string thePath = Path.ChangeExtension(Leayal.AppInfo.ApplicationFilename, ".update-" + ver.ToString()); this.RaiseEventStepChanged(LanguageManager.GetMessageText("SelfUpdate_ExtractingUpdates", "Extracting Updates")); using (FileStream fssss = new FileStream(thePath + ".7z", FileMode.Open, FileAccess.Read, FileShare.Read)) if (SharpCompress.Archives.SevenZip.SevenZipArchive.IsSevenZipFile(fssss)) { fssss.Position = 0; using (SharpCompress.Archives.SevenZip.SevenZipArchive archive = SharpCompress.Archives.SevenZip.SevenZipArchive.Open(fssss)) using (SharpCompress.Readers.IReader reader = archive.ExtractAllEntries()) while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { if (reader.Entry.Key.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { using (FileStream fs = File.Create(thePath)) reader.WriteEntryTo(fs); break; } } } } else { this.OnHandledException(new HandledExceptionEventArgs(new FileNotFoundException("Update content not found", thePath))); } try { File.Delete(thePath + ".7z"); } catch { } this.OnProgressBarStateChanged(new Events.ProgressBarStateChangedEventArgs(Forms.MyMainMenu.ProgressBarVisibleState.Infinite)); this.RaiseEventStepChanged(LanguageManager.GetMessageText("SelfUpdate_RestartToUpdate", "Restarting application to perform update.")); using (Process theProcess = new Process()) { theProcess.StartInfo.FileName = this.UpdaterPath; theProcess.StartInfo.Arguments = Leayal.ProcessHelper.TableStringToArgs( new string[] { "-leayal", $"-patch:{thePath}", $"-destination:{Leayal.AppInfo.ApplicationFilename}" } ); if (Leayal.OSVersionInfo.IsVistaAndUp) { theProcess.StartInfo.Verb = "runas"; } theProcess.Start(); } Environment.Exit(0); }