public MainWindowViewModel() { Samples = new ObservableCollection<string>(); RefreshSamplesCommand = new System.Wpf.Mvvm.Commands.AsyncDelegateCommand(async (t) => { var d = new DownloadDialog(); d.ShowDialog(); }, (obj) => true); foreach (var s in Directory.GetFiles(Environment.CurrentDirectory + "\\Samples\\", "*.dll")) { var ass = Assembly.LoadFile(s); foreach (var t in ass.GetTypes()) { if (t.BaseType.Name == typeof(Sample).Name) { var inst = ass.CreateInstance(t.FullName) as Sample; _samples.Add(inst.Name, inst.View); } } } foreach (var item in _samples) { Samples.Add(item.Key); } }
internal void UpdateAction() { if (Global.UpdateModel == null) { return; } var download = new DownloadDialog(); var result = download.ShowDialog(); if (result.HasValue && result.Value) { NotificationManager.RemoveNotification(s => s.Kind == StatusType.Update); //TODO: Check if possible to close. if (Dialog.Ask("ScreenToGif", LocalizationHelper.Get("Update.CloseThis"), LocalizationHelper.Get("Update.CloseThis.Detail"))) { Application.Current.Shutdown(69); } } }
private void HandleUri(string uri) { if (WindowState == FormWindowState.Minimized) { WindowState = FormWindowState.Normal; } Activate(); var fields = uri.Substring("smmm:".Length).Split(','); // TODO: lib-ify string itemType = fields.FirstOrDefault(x => x.StartsWith("gb_itemtype", StringComparison.InvariantCultureIgnoreCase)); itemType = itemType.Substring(itemType.IndexOf(":") + 1); string itemId = fields.FirstOrDefault(x => x.StartsWith("gb_itemid", StringComparison.InvariantCultureIgnoreCase)); itemId = itemId.Substring(itemId.IndexOf(":") + 1); var dummyInfo = new ModInfo(); using (var client = new UpdaterWebClient()) { var response = client.DownloadString( string.Format("https://api.gamebanana.com/Core/Item/Data?itemtype={0}&itemid={1}&fields=name,authors", itemType, itemId) ); var array = JsonConvert.DeserializeObject <string[]>(response); dummyInfo.Name = array[0]; var authors = JsonConvert.DeserializeObject <Dictionary <string, string[][]> >(array[1]); // for every array of string[] in authors, select the first element of each array var authorList = from i in (from x in authors select x.Value) from j in i select j[0]; dummyInfo.Author = string.Join(", ", authorList); } DialogResult result = MessageBox.Show(this, $"Do you want to install mod \"{dummyInfo.Name}\" by {dummyInfo.Author} from {new Uri(fields[0]).DnsSafeHost}?", "Mod Download", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result != DialogResult.Yes) { return; } string updatePath = Path.Combine("mods", ".updates"); #region create update folder do { try { result = DialogResult.Cancel; if (!Directory.Exists(updatePath)) { Directory.CreateDirectory(updatePath); } } catch (Exception ex) { result = MessageBox.Show(this, "Failed to create temporary update directory:\n" + ex.Message + "\n\nWould you like to retry?", "Directory Creation Failed", MessageBoxButtons.RetryCancel); } } while (result == DialogResult.Retry); #endregion string dummyPath = dummyInfo.Name; foreach (char c in Path.GetInvalidFileNameChars()) { dummyPath = dummyPath.Replace(c, '_'); } dummyPath = Path.Combine("mods", dummyPath); var updates = new List <ModDownload> { new ModDownload(dummyInfo, dummyPath, fields[0], null, 0) }; using (var progress = new DownloadDialog(updates, updatePath)) { progress.ShowDialog(this); } do { try { result = DialogResult.Cancel; Directory.Delete(updatePath, true); } catch (Exception ex) { result = MessageBox.Show(this, "Failed to remove temporary update directory:\n" + ex.Message + "\n\nWould you like to retry? You can remove the directory manually later.", "Directory Deletion Failed", MessageBoxButtons.RetryCancel); } } while (result == DialogResult.Retry); LoadModList(); }
internal bool InstallUpdate(bool wasPromptedManually = false) { try { //No new release available. if (Global.UpdateAvailable == null) { return(false); } //TODO: Check if Windows is not turning off. //Prompt if: //Not configured to download the update automatically OR //Configured to download but set to prompt anyway OR //Download not completed (perharps because the notification was triggered by a query on Fosshub). if (UserSettings.All.PromptToInstall || !UserSettings.All.InstallUpdates || string.IsNullOrWhiteSpace(Global.UpdateAvailable.InstallerPath)) { var download = new DownloadDialog { WasPromptedManually = wasPromptedManually }; var result = download.ShowDialog(); if (!result.HasValue || !result.Value) { return(false); } } //Only try to install if the update was downloaded. if (!File.Exists(Global.UpdateAvailable.InstallerPath)) { return(false); } var files = Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).ToList(); var isInstaller = files.Any(x => x.ToLowerInvariant().EndsWith("screentogif.visualelementsmanifest.xml")); var hasSharpDx = files.Any(x => x.ToLowerInvariant().EndsWith("sharpdx.dll")); var hasGifski = files.Any(x => x.ToLowerInvariant().EndsWith("gifski.dll")); var hasMenuShortcut = File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Microsoft", "Windows", "Start Menu", "Programs", "ScreenToGif.lnk")); var hasDesktopShortcut = File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Desktop", "ScreenToGif.lnk")); var startInfo = new ProcessStartInfo { FileName = "msiexec", Arguments = $" {(isInstaller ? "/i" : "/a")} \"{Global.UpdateAvailable.InstallerPath}\"" + $" {(isInstaller ? "INSTALLDIR" : "TARGETDIR")}=\"{AppDomain.CurrentDomain.BaseDirectory}\" INSTALLAUTOMATICALLY=yes INSTALLPORTABLE={(isInstaller ? "no" : "yes")}" + $" ADDLOCAL=Binary{(isInstaller ? ",Auxiliar" : "")}{(hasSharpDx ? ",SharpDX" : "")}{(hasGifski ? ",Gifski" : "")}" + $" {(wasPromptedManually ? "RUNAFTER=yes" : "")}" + (isInstaller ? $" INSTALLDESKTOPSHORTCUT={(hasDesktopShortcut ? "yes" : "no")} INSTALLSHORTCUT={(hasMenuShortcut ? "yes" : "no")}" : ""), Verb = "runas" }; using (var process = new Process { StartInfo = startInfo }) process.Start(); return(true); } catch (Exception ex) { LogWriter.Log(ex, "Impossible to automatically install update"); ErrorDialog.Ok("ScreenToGif", "It was not possible to install the update", ex.Message, ex); return(false); } }
/// <summary>更新程序</summary> /// <param name="isManual">是否为手动点击更新</param> private static void UpdateApp(bool isManual) { using (UAWebClient client = new UAWebClient()) { string url = AppConfig.RequestUseGithub ? AppConfig.GithubLatestApi : AppConfig.GiteeLatestApi; XmlDocument doc = client.GetWebJsonToXml(url); if (doc == null) { if (isManual) { if (AppMessageBox.Show(AppString.Message.WebDataReadFailed + "\r\n" + AppString.Message.OpenWebUrl, MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } url = AppConfig.RequestUseGithub ? AppConfig.GithubLatest : AppConfig.GiteeReleases; ExternalProgram.OpenWebUrl(url); } return; } XmlNode root = doc.FirstChild; XmlNode tagNameXN = root.SelectSingleNode("tag_name"); Version webVer = new Version(tagNameXN.InnerText); Version appVer = new Version(Application.ProductVersion); #if DEBUG appVer = new Version(0, 0, 0, 0);//测试用 #endif if (appVer >= webVer) { if (isManual) { AppMessageBox.Show(AppString.Message.VersionIsLatest, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { XmlNode bodyXN = root.SelectSingleNode("body"); string info = AppString.Message.UpdateInfo.Replace("%v1", appVer.ToString()).Replace("%v2", webVer.ToString()); info += "\r\n\r\n" + MachinedInfo(bodyXN.InnerText); if (MessageBox.Show(info, AppString.General.AppName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { string netVer = Environment.Version > new Version(4, 0) ? "4.0" : "3.5"; XmlNode assetsXN = root.SelectSingleNode("assets"); foreach (XmlNode itemXN in assetsXN.SelectNodes("item")) { XmlNode nameXN = itemXN.SelectSingleNode("name"); if (nameXN != null && nameXN.InnerText.Contains(netVer)) { XmlNode urlXN = itemXN.SelectSingleNode("browser_download_url"); using (DownloadDialog dlg = new DownloadDialog()) { dlg.Url = urlXN?.InnerText; dlg.FilePath = $@"{AppConfig.AppDataDir}\{webVer}.exe"; dlg.Text = AppString.General.AppName; if (dlg.ShowDialog() == DialogResult.OK) { AppMessageBox.Show(AppString.Message.UpdateSucceeded, MessageBoxButtons.OK, MessageBoxIcon.Information); SingleInstance.Restart(null, dlg.FilePath); } } } } } } } }