public static void InstallUpdate(ref UpdaterJob job, string updateDir) { foreach (string filePath in job.filesToAdd) { try { File.Copy(updateDir + "\\" + filePath, filePath, true); } catch (DirectoryNotFoundException) { Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOf("\\"))); File.Copy(updateDir + "\\" + filePath, filePath, true); } catch { Console.WriteLine("Failed to copy the file" + filePath); } } foreach (string filePath in job.filesToRemove) { try { File.Delete(updateDir + "\\" + filePath); } catch { Console.WriteLine("Failed to delete the file" + filePath); } } }
private static void AnalyseRelease(ref List <DirectoryInfo> releases) { List <DirectoryInfo> remainingUpdates = new List <DirectoryInfo>(releases); for (int i = 0; i < releases.Count; i++) { Console.WriteLine($"Installing extracted update {i + 1} of {releases.Count}"); string updateDir = releases[i].FullName; using (StreamReader reader = File.OpenText(updateDir + "\\updaterjob.json")) using (JsonTextReader treader = new JsonTextReader(reader)) { JObject jsonObj = (JObject)JToken.ReadFrom(treader); if (jsonObj["uUpdater"].ToObject <bool>() && uUpdated == false) { Console.WriteLine("Update contains an update for the updater"); Console.WriteLine(jsonObj["uUpdaterScriptPath"]); File.WriteAllText("remUpdates.json", JsonConvert.SerializeObject(remainingUpdates)); Process uupdate = new Process() { StartInfo = new ProcessStartInfo() { FileName = updateDir + "\\" + jsonObj["uUpdaterScriptPath"], WorkingDirectory = programDir, Arguments = Process.GetCurrentProcess().Id.ToString() + " " + releases[i].Name + "\\", Verb = "runas" } }; uupdate.Start(); Console.WriteLine("Started updater script"); Console.ReadKey(); } //jsonObj["updaterJobVersion"]; //UpdaterJob file version, useful for later when there's gonna be updates to the UpdaterJob itself UpdaterJob updaterJob = jsonObj["UpdaterJob"].ToObject <UpdaterJob>(); InstallUpdate(ref updaterJob, updateDir); } Directory.Delete(updateDir, true); Console.WriteLine($"Installed update {i + 1} of {releases.Count}{"\n"}"); remainingUpdates.RemoveAt(i); uUpdated = false; } Exit("Updates have finished installing"); }