diff --git a/EnvyUpdate/MainWindow.xaml.cs b/EnvyUpdate/MainWindow.xaml.cs index 4a5026b..c8a2626 100644 --- a/EnvyUpdate/MainWindow.xaml.cs +++ b/EnvyUpdate/MainWindow.xaml.cs @@ -19,22 +19,36 @@ namespace EnvyUpdate readonly string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup); string gpuURL = null; readonly string exeloc = System.Reflection.Assembly.GetEntryAssembly().Location; - readonly string exepath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\"; + readonly string exepath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\"; readonly string startmenu = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu); + readonly string version = "1.0"; public MainWindow() { InitializeComponent(); + Title += " " + version; if (Util.IsInstanceOpen("EnvyUpdate")) { MessageBox.Show("Application is already running."); - Application.Current.Shutdown(1); + Environment.Exit(1); } if (!Directory.Exists(appdata)) { Directory.CreateDirectory(appdata); } + try + { + if (Util.GetNewVer() != version && exepath == appdata) + { + Util.UpdateApp(); + } + } + catch (WebException) + { + //Silently fail. + } + if (Util.GetLocDriv() != null) { localDriv = Util.GetLocDriv(); @@ -43,7 +57,7 @@ namespace EnvyUpdate else { MessageBox.Show("No NVIDIA GPU found. Application will exit."); - Application.Current.Shutdown(255); + Environment.Exit(255); } if (File.Exists(appdata + "nvidia-update.txt")) { @@ -116,7 +130,7 @@ namespace EnvyUpdate { textblockOnline.Foreground = Brushes.Red; buttonDL.Visibility = Visibility.Visible; - Notify.ShowUpdatePopup(); + Notify.ShowDrivUpdatePopup(); } else textblockOnline.Foreground = Brushes.Green; diff --git a/EnvyUpdate/Notify.cs b/EnvyUpdate/Notify.cs index 88e4643..4754310 100644 --- a/EnvyUpdate/Notify.cs +++ b/EnvyUpdate/Notify.cs @@ -4,7 +4,7 @@ namespace EnvyUpdate { class Notify { - public static void ShowUpdatePopup() + public static void ShowDrivUpdatePopup() { var notificationManager = new NotificationManager(); diff --git a/EnvyUpdate/Util.cs b/EnvyUpdate/Util.cs index 37bc68c..f4a1ba1 100644 --- a/EnvyUpdate/Util.cs +++ b/EnvyUpdate/Util.cs @@ -5,6 +5,7 @@ using System.IO; using IWshRuntimeLibrary; using System.Diagnostics; using System.Windows; +using System.Net; namespace EnvyUpdate { @@ -105,5 +106,42 @@ namespace EnvyUpdate Application.Current.MainWindow.Show(); Application.Current.MainWindow.WindowState = WindowState.Normal; } + public static string GetNewVer() + { + string updPath = "https://raw.githubusercontent.com/fyr77/EnvyUpdate/master/res/version.txt"; + + System.Net.WebClient wc = new System.Net.WebClient(); + string webData = wc.DownloadString(updPath); + + return webData; + } + /// + /// Updates the application by downloading the new version from Github and replacing the old file using a seperate CMD instance. + /// + public static void UpdateApp() + { + string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\envyupdate\\"; + + using (var client = new WebClient()) + { + client.DownloadFile("https://github.com/fyr77/EnvyUpdate/releases/latest/download/EnvyUpdate.exe", appdata + "EnvyUpdated.exe"); + } + + MessageBox.Show("New version of EnvyUpdate found. Application will restart.\nThis will probably take a few seconds."); + + // Replace exe with new one + Process process = new Process(); + ProcessStartInfo startInfo = new ProcessStartInfo + { + WindowStyle = ProcessWindowStyle.Hidden, + WorkingDirectory = appdata, + FileName = "cmd.exe", + Arguments = "/C timeout 5 && del EnvyUpdate.exe && ren EnvyUpdated.exe EnvyUpdate.exe && EnvyUpdate.exe" + }; + process.StartInfo = startInfo; + process.Start(); + + Environment.Exit(2); + } } } diff --git a/res/version.txt b/res/version.txt new file mode 100644 index 0000000..d3827e7 --- /dev/null +++ b/res/version.txt @@ -0,0 +1 @@ +1.0