diff --git a/README.md b/README.md index fb0dd76..6e37df9 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,15 @@ # Updater -Light weight program updater that takes file update info as command line arguments. +Light weight file updater. ![alt tag](http://i.imgur.com/FztHxfx.png) -* Parameters are separated by spaces or new lines. -* First parameter expected is an executable to be run when the updater finishes. - - Ideally the program that calls the updater. - - This is auto generated by UpdateCheck.cs used in all of my programs. +* Feed update manifest url as command line argument. + - i.e. a URL to a text file. +* Update parameters are separated by new lines. +* First paramenter expected is the new package version. + - This is usually only needed for UpdateCheck.cs, which opens the Updater. +* Second parameter expected is an executable to be run when the updater finishes. + - Ideally the program that opens the updater, but can be anything. * Rest of parameters expected will be in pairs of two - File download URL - File path and/or File name @@ -14,9 +17,10 @@ Light weight program updater that takes file update info as command line argumen Example: ``` -SAM.exe -https://drive.google.com/uc?export=download&id=0B2byNRcR0k4vblQxNWNEakNISFU -SAM.exe -https://drive.google.com/uc?export=download&id=0B2byNRcR0k4veXpSUUktVTNZSms -Updater.exe +1.2.3.4 +fileToRunWhenComplete.exe +(file2 direct download URL) +file2.dll +(file3 direct download URL) +file3.txt ``` \ No newline at end of file diff --git a/Updater/MainWindow.xaml.cs b/Updater/MainWindow.xaml.cs index a6634c5..20af422 100644 --- a/Updater/MainWindow.xaml.cs +++ b/Updater/MainWindow.xaml.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using System.IO; using System.Net; +using System.Net.Http; +using System.Text; using System.Threading.Tasks; using System.Windows; @@ -30,43 +32,86 @@ public MainWindow() { InitializeComponent(); - this.Loaded += MainWindow_Loaded; + this.Loaded += MainWindow_LoadedAsync; } - private void MainWindow_Loaded(object sender, RoutedEventArgs e) + private async void MainWindow_LoadedAsync(object sender, RoutedEventArgs e) { string[] args = Environment.GetCommandLineArgs(); List arguments = new List(); - if (args.Length < 4) + // There is always one argument by default + // and there is only one additional argument expected. + if (args.Length < 2) { - MessageBox.Show("No arguments found. Please run the updater with command line arguments."); - Environment.Exit(1); + MessageBox.Show("No arguments found. Please provide update manifest."); + Close(); } - string executable = args[1]; + string manifestUrl = args[1]; - // Get file download info from command line args. - for (int i = 2; i < args.Length; i += 2) + ConsoleBox.AppendText("Reading update manifest . . .\n"); + + // Open the text file using a stream reader. + using (Stream stream = await new HttpClient().GetStreamAsync(manifestUrl)) { - // If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs - if (args[i + 1] == "Updater.exe") + StreamReader reader = new StreamReader(stream); + + // Initialize variables. + Version latest = null; + string executable = null; + + try { - arguments.Add(new Download { url = args[i], file = "Updater_new.exe" }); + // First two expected items in manifest will be the new version and the starting executable. + latest = Version.Parse(await reader.ReadLineAsync()); + executable = await reader.ReadLineAsync(); } - else - { - arguments.Add(new Download { url = args[i], file = args[i + 1] }); + catch (Exception m) + { + MessageBox.Show("Error parsing new version number or launch executable."); + Close(); } - } - - if (arguments.Count == 0) - { - MessageBox.Show("No arguments found. Please run the updater with command line arguments."); - Environment.Exit(1); - } - Update(executable, arguments); + // Load manifest urls and file names. + while (!reader.EndOfStream) + { + try + { + // Expected in pairs of two. + string downloadUrl = await reader.ReadLineAsync(); + string downloadFile = await reader.ReadLineAsync(); + + if (downloadUrl == null || downloadFile == null) + { + MessageBox.Show("Uneven update manifest.\nPlease review format."); + Close(); + } + + // If a new version of the updater is being acquired, rename it to be handled in UpdateCheck.cs + if (downloadFile == "Updater.exe") + { + arguments.Add(new Download { url = downloadUrl, file = "Updater_new.exe" }); + } + else + { + arguments.Add(new Download { url = downloadUrl, file = downloadFile }); + } + } + catch (Exception m) + { + MessageBox.Show("Uneven update manifest.\nPlease review format."); + Close(); + } + } + if (arguments.Count == 0) + { + MessageBox.Show("No download arguments found in manifest."); + Close(); + } + + Update(executable, arguments); + } } private async void Update(string executable, List downloads) @@ -80,11 +125,11 @@ private async void Update(string executable, List downloads) ConsoleBox.AppendText("Update complete!\n"); MessageBox.Show("Update complete!"); - // Open updated program. - // The last executable file to be downloaded will be executed. - + // First argument is the program to be opened when the update is complete. if (executable != null && executable.Length > 1 && executable.Contains(".exe")) { + ConsoleBox.AppendText("Attempting to start" + executable + " . . .\n"); + try { Process.Start(executable); diff --git a/Updater/Properties/AssemblyInfo.cs b/Updater/Properties/AssemblyInfo.cs index 90d645a..34c0473 100644 --- a/Updater/Properties/AssemblyInfo.cs +++ b/Updater/Properties/AssemblyInfo.cs @@ -1,6 +1,4 @@ using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows; @@ -51,5 +49,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")]