Ejemplo n.º 1
0
        /// <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);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>重新加载字典</summary>
        public static void ReloadDics()
        {
            XmlDocument LoadXml(string xmlPath)
            {
                if (!File.Exists(xmlPath))
                {
                    return(null);
                }
                try
                {
                    XmlDocument doc      = new XmlDocument();
                    Encoding    encoding = EncodingType.GetType(xmlPath);
                    string      xml      = File.ReadAllText(xmlPath, encoding).Trim();
                    doc.LoadXml(xml);
                    return(doc);
                }
                catch (Exception e)
                {
                    AppMessageBox.Show(e.Message + "\n" + xmlPath);
                    return(null);
                }
            }

            void LoadDic(List <XmlDocument> dic, string webPath, string userPath, string defaultContent)
            {
                if (!File.Exists(webPath))
                {
                    File.WriteAllText(webPath, defaultContent, Encoding.Unicode);
                }
                dic.Clear();
                dic.Add(LoadXml(webPath));
                dic.Add(LoadXml(userPath));
            }

            LoadDic(UwpModeItemsDic, AppConfig.WebUwpModeItemsDic,
                    AppConfig.UserUwpModeItemsDic, Properties.Resources.UwpModeItemsDic);
            LoadDic(EnhanceMenusDic, AppConfig.WebEnhanceMenusDic,
                    AppConfig.UserEnhanceMenusDic, Properties.Resources.EnhanceMenusDic);
            LoadDic(DetailedEditDic, AppConfig.WebDetailedEditDic,
                    AppConfig.UserDetailedEditDic, Properties.Resources.DetailedEditDic);

            EnhanceMenuPathDic.Clear();
            for (int i = 0; i < 2; i++)
            {
                XmlDocument doc = EnhanceMenusDic[i];
                if (doc?.DocumentElement == null)
                {
                    continue;
                }
                foreach (XmlNode pathXN in doc.SelectNodes("Data/Group/RegPath"))
                {
                    if (EnhanceMenuPathDic.ContainsKey(pathXN.InnerText))
                    {
                        continue;
                    }
                    EnhanceMenuPathDic.Add(pathXN.InnerText, i == 1);
                }
            }

            DetailedEditGuidDic.Clear();
            for (int i = 0; i < 2; i++)
            {
                XmlDocument doc = DetailedEditDic[i];
                if (doc?.DocumentElement == null)
                {
                    continue;
                }
                foreach (XmlNode guidXN in doc.SelectNodes("Data/Group/Guid"))
                {
                    if (GuidEx.TryParse(guidXN.InnerText, out Guid guid))
                    {
                        if (DetailedEditGuidDic.ContainsKey(guid))
                        {
                            continue;
                        }
                        DetailedEditGuidDic.Add(guid, i == 1);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>更新程序字典</summary>
        /// <param name="isManual">是否为手动点击更新</param>
        private static void UpdateText(bool isManual)
        {
            string dirUrl;

            string[] filePaths;
            void WriteFiles(string dirName, out string succeeded, out string failed)
            {
                succeeded = failed = "";
                foreach (string filePath in filePaths)
                {
                    using (UAWebClient client = new UAWebClient())
                    {
                        string fileUrl = $"{dirUrl}/{Path.GetFileName(filePath)}";
                        var    func    = new Func <string, string, bool>(client.WebStringToFile);
                        bool   flag    = func.EndInvoke(func.BeginInvoke(filePath, fileUrl, null, null));
                        string item    = "\r\n ● " + Path.GetFileName(filePath);
                        if (flag)
                        {
                            succeeded += item;
                        }
                        else
                        {
                            failed += item;
                        }
                    }
                }
                dirName = "\r\n\r\n" + dirName + ":";
                if (succeeded != "")
                {
                    succeeded = dirName + succeeded;
                }
                if (failed != "")
                {
                    failed = dirName + failed;
                }
            }

            dirUrl    = AppConfig.RequestUseGithub ? AppConfig.GithubTexts : AppConfig.GiteeTexts;
            filePaths = new[]
            {
                AppConfig.WebGuidInfosDic, AppConfig.WebEnhanceMenusDic,
                AppConfig.WebDetailedEditDic, AppConfig.WebUwpModeItemsDic
            };
            WriteFiles("Dictionaries", out string succeeded1, out string failed1);

            dirUrl    = AppConfig.RequestUseGithub ? AppConfig.GithubLangsRawDir : AppConfig.GiteeLangsRawDir;
            filePaths = Directory.GetFiles(AppConfig.LangsDir, "*.ini");
            WriteFiles("Languages", out string succeeded2, out string failed2);

            if (isManual)
            {
                string failed    = failed1 + failed2;
                string succeeded = succeeded1 + succeeded2;
                if (failed != "")
                {
                    AppMessageBox.Show(AppString.Message.WebDataReadFailed + failed);
                }
                if (succeeded != "")
                {
                    AppMessageBox.Show(AppString.Message.DicUpdateSucceeded + succeeded,
                                       MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }