private static CheckedEventArgs Check(string curVersion, string updaterUrl) { CheckedEventArgs args = new CheckedEventArgs(); try { HttpWebRequest reqUrl = (HttpWebRequest)WebRequest.Create(updaterUrl); using (HttpWebResponse respUrl = (HttpWebResponse)reqUrl.GetResponse()) { using (Stream stream = respUrl.GetResponseStream()) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(stream); args.Version = xmlDoc.SelectSingleNode("item/version").InnerText.Trim(); args.FileUrl = xmlDoc.SelectSingleNode("item/url").InnerText.Trim(); args.Code = args.Version == "0.0.0.0" || args.Version.Equals(curVersion) ? "Unavailable" : "Available"; args.Desc = args.Version == "0.0.0.0" || args.Version.Equals(curVersion) ? "无可用更新版本" : "有可用更新版本"; } } } catch (Exception ex) { args.Exception = ex; } return(args); }
private static void DoWork() { CheckedEventArgs argsCheck = Check(Constants.CurVersion, Constants.UpdaterUrl); if (argsCheck.Exception != null) { if (null != OnEnd) { OnEnd(argsCheck.Exception, false); } return; } if (argsCheck.Code == "Unavailable") { if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = argsCheck.Desc, Percent = 100 }); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Progressing", Desc = argsCheck.Desc, Percent = 33 }); } DownloadedEventArgs argsDownload = Download(argsCheck.FileUrl); if (argsDownload.Exception != null) { if (null != OnEnd) { OnEnd(argsDownload.Exception, false); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Progressing", Desc = argsDownload.Desc, Percent = 66 }); } ResultEventArgs argsResult = null; if (argsDownload.FilePath.EndsWith(".zip")) { argsResult = UnZip(argsDownload.FilePath); } else if (argsDownload.FilePath.EndsWith(".cab")) { argsResult = ExecuteCab(argsDownload.FilePath); } if (null == argsResult) { if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = "升级成功", Percent = 100 }); } return; } if (argsResult.Exception != null) { if (null != OnEnd) { OnEnd(argsResult.Exception, false); } return; } if (null != OnProgress) { OnProgress(new ProgressEventArgs { Code = "Finished", Desc = argsResult.Desc, Percent = 100, NoRun = argsResult.NoRun }); } }