private static byte[] GetWebShellNewData(string extension)
        {
            string apiUrl = AppConfig.RequestUseGithub ? AppConfig.GithubShellNewApi : AppConfig.GiteeShellNewApi;

            using (UAWebClient client = new UAWebClient())
            {
                XmlDocument doc = client.GetWebJsonToXml(apiUrl);
                if (doc == null)
                {
                    return(null);
                }
                foreach (XmlNode node in doc.FirstChild.ChildNodes)
                {
                    XmlNode nameXN = node.SelectSingleNode("name");
                    string  str    = Path.GetExtension(nameXN.InnerText);
                    if (string.Equals(str, extension, StringComparison.OrdinalIgnoreCase))
                    {
                        try
                        {
                            string dirUrl  = AppConfig.RequestUseGithub ? AppConfig.GithubShellNewRawDir : AppConfig.GiteeShellNewRawDir;
                            string fileUrl = $"{dirUrl}/{nameXN.InnerText}";
                            return(client.DownloadData(fileUrl));
                        }
                        catch { return(null); }
                    }
                }
                return(null);
            }
        }
 private void ShowDonateDialog()
 {
     this.Cursor = Cursors.WaitCursor;
     using (UAWebClient client = new UAWebClient())
     {
         string url      = AppConfig.RequestUseGithub ? AppConfig.GithubDonateRaw : AppConfig.GiteeDonateRaw;
         string contents = client.GetWebString(url);
         //contents = System.IO.File.ReadAllText(@"..\..\..\Donate.md");//用于求和更新Donate.md文件
         if (contents == null)
         {
             if (AppMessageBox.Show(AppString.Message.WebDataReadFailed + "\r\n"
                                    + AppString.Message.OpenWebUrl, MessageBoxButtons.OKCancel) == DialogResult.OK)
             {
                 url = AppConfig.RequestUseGithub ? AppConfig.GithubDonate : AppConfig.GiteeDonate;
                 ExternalProgram.OpenWebUrl(url);
             }
         }
         else
         {
             using (DonateListDialog dlg = new DonateListDialog())
             {
                 dlg.DanateData = contents;
                 dlg.ShowDialog();
             }
         }
     }
     this.Cursor = Cursors.Default;
 }
示例#3
0
 /// <summary>获取网页文本</summary>
 private static string GetWebString(string url)
 {
     try
     {
         using (UAWebClient client = new UAWebClient())
         {
             return(client.DownloadString(url));
         }
     }
     catch { return(null); }
 }
示例#4
0
 /// <summary>获取网页文本</summary>
 private static string GetWebString(string url)
 {
     try
     {
         using (UAWebClient client = new UAWebClient())
         {
             string str = client.DownloadString(url);
             str = str?.Replace("\n", Environment.NewLine);//换行符转换
             return(str);
         }
     }
     catch { return(null); }
 }
示例#5
0
 /// <summary>获取网页Json文本并加工为Xml</summary>
 private static XmlDocument GetWebJsonToXml(string url)
 {
     try
     {
         using (UAWebClient client = new UAWebClient())
         {
             byte[] bytes = client.DownloadData(url);
             using (XmlReader xReader = JsonReaderWriterFactory.CreateJsonReader(bytes, XmlDictionaryReaderQuotas.Max))
             {
                 XmlDocument doc = new XmlDocument();
                 doc.Load(xReader);
                 return(doc);
             }
         }
     }
     catch { return(null); }
 }
 public void ShowLanguageDialog()
 {
     using (UAWebClient client = new UAWebClient())
     {
         string      apiUrl = AppConfig.RequestUseGithub ? AppConfig.GithubLangsApi : AppConfig.GiteeLangsApi;
         XmlDocument doc    = client.GetWebJsonToXml(apiUrl);
         if (doc == null)
         {
             AppMessageBox.Show(AppString.Message.WebDataReadFailed);
             return;
         }
         XmlNodeList list  = doc.FirstChild.ChildNodes;
         string[]    langs = new string[list.Count];
         for (int i = 0; i < list.Count; i++)
         {
             XmlNode nameXN = list.Item(i).SelectSingleNode("name");
             langs[i] = Path.GetFileNameWithoutExtension(nameXN.InnerText);
         }
         if (langs.Length == 0)
         {
             AppMessageBox.Show(AppString.Message.WebDataReadFailed);
             return;
         }
         using (SelectDialog dlg = new SelectDialog())
         {
             dlg.Items = langs;
             dlg.Title = AppString.Dialog.DownloadLanguages;
             string lang = CultureInfo.CurrentUICulture.Name;
             if (dlg.Items.Contains(lang))
             {
                 dlg.Selected = lang;
             }
             else
             {
                 dlg.SelectedIndex = 0;
             }
             if (dlg.ShowDialog() == DialogResult.OK)
             {
                 string fileName = $"{dlg.Selected}.ini";
                 string filePath = $@"{AppConfig.LangsDir}\{fileName}";
                 string dirUrl   = AppConfig.RequestUseGithub ? AppConfig.GithubLangsRawDir : AppConfig.GiteeLangsRawDir;
                 string fileUrl  = $"{dirUrl}/{fileName}";
                 bool   flag     = client.WebStringToFile(filePath, fileUrl);
                 if (!flag)
                 {
                     if (AppMessageBox.Show(AppString.Message.WebDataReadFailed + "\r\n ● " + fileName + "\r\n"
                                            + AppString.Message.OpenWebUrl, MessageBoxButtons.YesNo) == DialogResult.Yes)
                     {
                         ExternalProgram.OpenWebUrl(fileUrl);
                     }
                 }
                 else
                 {
                     this.LoadLanguages();
                     string language = new IniWriter(filePath).GetValue("General", "Language");
                     if (language == "")
                     {
                         language = dlg.Selected;
                     }
                     cmbLanguages.Text = language;
                     ChangeLanguage();
                 }
             }
         }
     }
 }
        /// <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);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <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);
                }
            }
        }