/// <summary> /// 检查是否有更新的返回及处理 /// </summary> /// <param name="result"></param> private void EndGetCheckResponse(IAsyncResult result) { UpdateInfo ui = new UpdateInfo(); ui.UpdateFilesInfo = new List<UpdateFileInfo>(); try { //结束异步请求,获取结果 HttpWebRequest webRequest = (HttpWebRequest)result.AsyncState; WebResponse webResponse = webRequest.EndGetResponse(result); //把输出结果转化为Person对象 Stream stream = webResponse.GetResponseStream(); StreamReader sr = new StreamReader(stream, Encoding.UTF8); string upInfo = sr.ReadToEnd(); //开始解析更新文件的xml if (upInfo.Trim().Length > 0) { XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(upInfo); XmlNode root = xmldoc.SelectSingleNode("//files"); if (root.HasChildNodes) { ui.FilesTotalSize = int.Parse(root.Attributes["totalsize"].Value); ui.FilesTotalSizeUnit = root.Attributes["unit"].Value; XmlNodeList xnl = root.ChildNodes; foreach (XmlNode xn in xnl) { UpdateFileInfo ufi = new UpdateFileInfo(); string ufileVersion = xn.Attributes["version"].Value; string[] ufileVerInfo = ufileVersion.Split(new char[] { '.' }); FileVersionInfo fileVer = FileVersionInfo.GetVersionInfo(xn.Attributes["name"].Value); string oldFileVersion = fileVer.FileVersion; string[] oldFileVerInfo = oldFileVersion.Split(new char[] { '.' }); //以下开始比较文件版本号。如果更新文件中的文件版本号大于本地的文件版本号,则下载更新,否则,不下载此文件更新 if (int.Parse(ufileVerInfo[0]) > int.Parse(oldFileVerInfo[0]) || int.Parse(ufileVerInfo[1]) > int.Parse(oldFileVerInfo[1]) || int.Parse(ufileVerInfo[2]) > int.Parse(oldFileVerInfo[2]) || int.Parse(ufileVerInfo[3]) > int.Parse(oldFileVerInfo[3])) { ufi.FileName = xn.Attributes["name"].Value; ufi.FileSize = int.Parse(xn.Attributes["size"].Value); ufi.FileVersion = xn.Attributes["version"].Value; ui.UpdateFilesInfo.Add(ufi); } } if (ui.UpdateFilesInfo.Count == 0) { ui.CheckResult = "您目前的版本已是最新"; } } else { ui.CheckResult = "您目前的版本已是最新"; } } else { ui.CheckResult = "您目前的版本已是最新"; } } catch (Exception e) { ui.CheckResult = "更新检查无法完成,请联系供应商"; } this.uInfo = ui; if (sueh != null) sueh(ui); }
/// <summary> /// 检查是否有更新的返回及处理 /// </summary> /// <param name="result"></param> private void EndGetCheckResponse(IAsyncResult result) { UpdateInfo ui = new UpdateInfo(); ui.UpdateFilesInfo = new List <UpdateFileInfo>(); try { //结束异步请求,获取结果 HttpWebRequest webRequest = (HttpWebRequest)result.AsyncState; WebResponse webResponse = webRequest.EndGetResponse(result); //把输出结果转化为Person对象 Stream stream = webResponse.GetResponseStream(); StreamReader sr = new StreamReader(stream, Encoding.UTF8); string upInfo = sr.ReadToEnd(); //开始解析更新文件的xml if (upInfo.Trim().Length > 0) { XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(upInfo); XmlNode root = xmldoc.SelectSingleNode("//files"); if (root.HasChildNodes) { ui.FilesTotalSize = int.Parse(root.Attributes["totalsize"].Value); ui.FilesTotalSizeUnit = root.Attributes["unit"].Value; XmlNodeList xnl = root.ChildNodes; foreach (XmlNode xn in xnl) { UpdateFileInfo ufi = new UpdateFileInfo(); string ufileVersion = xn.Attributes["version"].Value; string[] ufileVerInfo = ufileVersion.Split(new char[] { '.' }); FileVersionInfo fileVer = FileVersionInfo.GetVersionInfo(xn.Attributes["name"].Value); string oldFileVersion = fileVer.FileVersion; string[] oldFileVerInfo = oldFileVersion.Split(new char[] { '.' }); //以下开始比较文件版本号。如果更新文件中的文件版本号大于本地的文件版本号,则下载更新,否则,不下载此文件更新 if (int.Parse(ufileVerInfo[0]) > int.Parse(oldFileVerInfo[0]) || int.Parse(ufileVerInfo[1]) > int.Parse(oldFileVerInfo[1]) || int.Parse(ufileVerInfo[2]) > int.Parse(oldFileVerInfo[2]) || int.Parse(ufileVerInfo[3]) > int.Parse(oldFileVerInfo[3])) { ufi.FileName = xn.Attributes["name"].Value; ufi.FileSize = int.Parse(xn.Attributes["size"].Value); ufi.FileVersion = xn.Attributes["version"].Value; ui.UpdateFilesInfo.Add(ufi); } } if (ui.UpdateFilesInfo.Count == 0) { ui.CheckResult = "您目前的版本已是最新"; } } else { ui.CheckResult = "您目前的版本已是最新"; } } else { ui.CheckResult = "您目前的版本已是最新"; } } catch (Exception e) { ui.CheckResult = "更新检查无法完成,请联系供应商"; } this.uInfo = ui; if (sueh != null) { sueh(ui); } }
private void ShowUpdateInfo(UpdateInfo ui) { //如果有更新,则提示用户 if (ui.FilesTotalSize != 0 && String.IsNullOrEmpty(ui.CheckResult)) { this.BeginInvoke(new MethodInvoker(delegate() { if (MessageBox.Show("发现软件有更新版本,是否下载更新?", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK) { Process.Start("Update.exe"); } })); } }