public static void Main() { // Removes updater temporary dir if (Directory.Exists(Path.Combine(Application.StartupPath, "SU_temp"))) { Directory.Delete(Path.Combine(Application.StartupPath, "SU_temp"), true); } // Cleanup if new version installed before start app var cleanNeeded = Directory.GetDirectories(Application.StartupPath) .Where(dir => Path.GetFileName(dir).EndsWith("_new")) .ToList() .AddToEnd( Directory.GetFiles(Application.StartupPath) .Where(file => Path.GetFileNameWithoutExtension(file).EndsWith("_new"))) .Count != 0; if (cleanNeeded) { var cleanOpts = new CleanUpOptions { CleanUpDir = Application.StartupPath, MergeSuffix = "new" }; Updater.Run(cleanOpts); } _localLatestVersion = GetLatest(); if (_localLatestVersion is null) { Environment.Exit(1); // exits if no versions installed. } // Check update var updateThread = new Thread(CheckAndDownload); updateThread.Start(); // Run app var exePath = Path.Combine(Application.StartupPath, _localLatestVersion.GetVersionString(), "ListenToIt.App.exe"); var appProc = new Process { StartInfo = new ProcessStartInfo { FileName = exePath, UseShellExecute = true, WorkingDirectory = exePath.Replace(Path.GetFileName(exePath), string.Empty) } }; appProc.Start(); appProc.WaitForExit(); }
private static void CheckAndDownload() { // call this method in a new thread // Checks for internet connection try { using (var client = new WebClient()) client.OpenRead(new Uri("https://github.com")); } catch { return; } Directory.CreateDirectory(Path.Combine(Application.StartupPath, "./SUCache")); // Creates dir for update cache var options = new CheckOptions { CheckOnly = true, CurrentVersion = _localLatestVersion, DownloadDir = Path.Combine(Application.StartupPath, "SUCache"), IncludePrerelease = true }; // Checks for update _updateAvailable = Updater.Run(options) == 200; if (!_updateAvailable) { return; } // Download options.CheckOnly = false; _updateDownloaded = Updater.Run(options) == 0; // Gets the latest downloaded version var downloadedPkgInfo = Directory.GetFiles(Path.Combine(Application.StartupPath, "SUCache")) .Where(file => Path.GetExtension(file) == ".json") .Where(file => Version.IsValidRawVersionString(Path.GetFileNameWithoutExtension(file) .Replace("package_info_", string.Empty))) .ToList(); _serverLatestVersion = new Version(Path.GetFileNameWithoutExtension(downloadedPkgInfo[0]) .Replace("package_info_", string.Empty)); for (var i = 1; i < downloadedPkgInfo.Count; i++) { var ver = new Version[] { new Version(Path.GetFileNameWithoutExtension(downloadedPkgInfo[i - 1]) .Replace("package_info_", "")), new Version(Path.GetFileNameWithoutExtension(downloadedPkgInfo[i]) .Replace("package_info_", "")) }; _serverLatestVersion = ver[1].IsNewerThan(ver[0]) ? ver[1] : _serverLatestVersion; } if (!_updateDownloaded) { return; // Exits if not downloaded } // Install update var installOpts = new InstallOptions { InstallDir = Application.StartupPath, PackagePath = Path.Combine(Application.StartupPath, "SUCache", $"package_info_{_serverLatestVersion.GetVersionString()}.json"), RemoveAfterInstall = false }; Updater.Run(installOpts); }