public static void CloseSplashScreen() { try { splashScreen?.Close(); splashScreen = null; } catch (NullReferenceException) { Logger.Debug("splashScreen was null."); } }
public static async Task CheckForUpdates() { if (Config.AppWasUpdated) { Logger.Info("Application was updated last time it ran, cleaning up."); if (File.Exists("./PingLogger-old.exe")) { File.Delete("./PingLogger-old.exe"); } if (Config.LastTempDir != string.Empty && Directory.Exists(Config.LastTempDir)) { File.Delete(Config.LastTempDir + "/PingLogger-Setup.msi"); Directory.Delete(Config.LastTempDir); Config.LastTempDir = string.Empty; } Config.AppWasUpdated = false; } else { if (Config.UpdateLastChecked.Date >= DateTime.Today) { Logger.Info("Application already checked for update today, skipping."); return; } splashScreen = new Controls.SplashScreen(); splashScreen.Show(); splashScreen.dlProgress.IsIndeterminate = true; splashScreen.dlProgress.Value = 1; var localVersion = Assembly.GetExecutingAssembly().GetName().Version; try { var httpClient = new WebClient(); bool downloadComplete = false; httpClient.DownloadFileCompleted += (o, i) => { downloadComplete = true; }; string azureURL = "https://pingloggerfiles.blob.core.windows.net/"; await httpClient.DownloadFileTaskAsync($"{azureURL}version/latest.json", $"./latest.json"); while (!downloadComplete) { await Task.Delay(100); } var latestJson = File.ReadAllText("./latest.json"); var remoteVersion = JsonSerializer.Deserialize <SerializableVersion>(latestJson); File.Delete("./latest.json"); Logger.Info($"Remote version is {remoteVersion}, currently running {localVersion}"); if (remoteVersion > localVersion) { Logger.Info("Remote contains a newer version"); if (Controls.UpdatePromptDialog.Show()) { if (Config.IsInstalled) { Config.LastTempDir = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Temp\\{RandomString(8)}"; Directory.CreateDirectory(Config.LastTempDir); Logger.Info($"Creating temporary path {Config.LastTempDir}"); Logger.Info($"Downloading newest installer to {Config.LastTempDir}\\PingLogger-Setup.msi"); var downloadURL = $"{azureURL}v{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/PingLogger-Setup.msi"; Logger.Info($"Downloading from {downloadURL}"); using var downloader = new HttpClientDownloadWithProgress(downloadURL, Config.LastTempDir + "\\PingLogger-Setup.msi"); splashScreen.mainLabel.Text = $"Downloading PingLogger setup v{remoteVersion}"; downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); Config.AppWasUpdated = true; Logger.Info("Uninstalling current version."); string batchFile = $@"@echo off msiexec.exe /q /l* '{ AppContext.BaseDirectory}Logs\Installer - v{localVersion}.log' /x {Config.InstallerGUID} msiexec.exe /l* '{ AppContext.BaseDirectory}Logs\Installer - v{remoteVersion}.log' /i {Config.LastTempDir}/PingLogger-Setup.msi"; File.WriteAllText(Config.LastTempDir + "/install.bat", batchFile); Process.Start(new ProcessStartInfo { FileName = "cmd.exe", UseShellExecute = true, Arguments = $"{Config.LastTempDir}/install.bat" }); Logger.Info("Installer started, closing."); Environment.Exit(0); } else { Logger.Info("Renamed PingLogger.exe to PingLogger-old.exe"); File.Move("./PingLogger.exe", "./PingLogger-old.exe"); Logger.Info("Downloading new PingLogger.exe"); var downloadURL = $"{azureURL}v{remoteVersion.Major}{remoteVersion.Minor}{remoteVersion.Build}/PingLogger.exe"; Logger.Info($"Downloading from {downloadURL}"); using var downloader = new HttpClientDownloadWithProgress(downloadURL, "./PingLogger.exe"); splashScreen.mainLabel.Text = $"Downloading PingLogger v{remoteVersion}"; downloader.ProgressChanged += Downloader_ProgressChanged; await downloader.StartDownload(); Config.AppWasUpdated = true; Process.Start(new ProcessStartInfo { FileName = "./PingLogger.exe" }); Logger.Info("Starting new version of PingLogger"); Environment.Exit(0); } } } } catch (HttpRequestException ex) { Logger.Error("Unable to auto update: " + ex.Message); return; } } Config.UpdateLastChecked = DateTime.Now; return; }