private string[] DeleteFiles() { FileIndex index = FileIndex.Deserialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); List <string> failedToRemove = new List <string>(); string[] filesSortedByDepth = index.files.OrderByDescending(path => path.Split(new Char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries).Length).ToArray(); foreach (string cur in filesSortedByDepth) //Delete files, deepest first, topmost last. { try { string curFile = InstallationSettings.InstallationFolder + '/' + cur; System.IO.File.Delete(curFile); DirectoryInfo subDirInfo = Directory.GetParent(curFile); if (subDirInfo.GetFiles().Length == 0 && subDirInfo.GetDirectories().Length == 0) { subDirInfo.Delete(); } } catch (Exception) { failedToRemove.Add(cur); } } System.IO.File.Delete(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); DirectoryInfo dirInfo = new DirectoryInfo(InstallationSettings.InstallationFolder); if (dirInfo.GetFiles().Length == 0 && dirInfo.GetDirectories().Length == 0) { dirInfo.Delete(); } return(failedToRemove.ToArray()); }
public void ApplyUpdate(AppInfo appInfo, UpdateInfo info, UpdateHost updateInfoSource) { string lockFile = InstallationSettings.InstallationFolder + "/Updater.lock"; if (File.Exists(lockFile)) { throw new Exception("Could not update: the application folder is already locked by another updater instance."); } else { File.Create(lockFile).Close(); } TriggerProgressEvent(0, "Calculating updater tasks"); FileIndex index = FileIndex.Deserialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); UpdateTask[] tasks = GetUpdaterTasks(appInfo, info, updateInfoSource, index); List <UpdateTask> failedTasks = RunTasks(tasks, true, 10); TriggerProgressEvent(percentageDone, "Retrying failed tasks"); for (int i = 0; i < failedTasks.Count; i++) { UpdateTask task = failedTasks[i]; try { task.Run(); } catch (WebException ex) { if (MessageBox.Show("Updater could not download file: " + ex.Message + "\r\n(" + ex.Data["Target"] + ")", "Failed to download file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error).Equals(DialogResult.Retry)) { i--; //Retry } else { Application.Exit(); } } catch (UnauthorizedAccessException ex) { if (MessageBox.Show("Updater could access file or folder: " + ex.Message + "\r\n(" + ex.Data["Target"] + ")", "Failed to download file", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error).Equals(DialogResult.Retry)) { i--; //Retry } else { Application.Exit(); } } } TriggerProgressEvent(98, "Updating local file index"); index = new FileIndex(index.appID); index.applicationVersion = info.version; index.files.AddRange(info.fileChecksums.Keys); index.Serialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); File.Delete(lockFile); }
private void StartUpdateCheck() { Updater updater = new Updater(); updater.RetrieveAppInfo((appInfo, hostURL) => { BeginInvoke((Action)(() => { if (appInfo == null) { if (MessageBox.Show(this, "Cannot check for updates: No valid update mirrors online. Launch application anyway?", "Failed to check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Warning).Equals(DialogResult.Yes)) { Program.LaunchTargetApplication(this); } this.Close(); return; } if (!File.Exists(InstallationSettings.InstallationFolder + "/UpdateIndex.dat")) { if (MessageBox.Show(this, "The installation seems to be corrupt: UpdateIndex.dat is missing. Launch application anyway?", "Failed to check for updates", MessageBoxButtons.YesNo, MessageBoxIcon.Error).Equals(DialogResult.Yes)) { Program.LaunchTargetApplication(this); } this.Close(); return; } FileIndex index = FileIndex.Deserialize(InstallationSettings.InstallationFolder + "/UpdateIndex.dat"); if (index.applicationVersion < appInfo.LatestVersion) { progressLabel.Text = LauncherLocale.Current.Get("Updater.Main.RetrievingUpdateInfo"); double[] newVersions = GetUpdatedVersions(appInfo, index.applicationVersion, appInfo.LatestVersion); MetroForm form; if (newVersions.Length == 1) { form = new SingleUpdateDetailsForm(updater, appInfo, hostURL, newVersions[0]); } else { form = new MultiUpdateDetailsForm(updater, appInfo, hostURL, newVersions); } this.Hide(); form.ShowDialog(); } progressLabel.Text = LauncherLocale.Current.Get("Updater.Main.LaunchApplication"); Program.LaunchTargetApplication(this); this.Close(); })); }); }