/// <summary> /// 检查更新文件 /// </summary> /// <param name="serverXmlFile">服务端版本文件XML</param> /// <param name="localXmlFile">客户端版本文件XML</param> /// <param name="updateFileList">更新文件列表</param> /// <returns>-1:服务端或客户端更新文件缺失。</returns> public int CheckForUpdate(string serverXmlFile, string localXmlFile, out Hashtable updateFileList) { updateFileList = new Hashtable(); if (!File.Exists(localXmlFile) || !File.Exists(serverXmlFile)) { return(-1); } XmlFiles serverXmlFiles = new XmlFiles(serverXmlFile); XmlFiles localXmlFiles = new XmlFiles(localXmlFile); XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files"); XmlNodeList oldNodeList = localXmlFiles.GetNodeList("AutoUpdater/Files"); int result = 0; for (int i = 0; i < newNodeList.Count; i++) { string[] fileList = new string[3]; string newFileName = newNodeList.Item(i).Attributes["Name"].Value.Trim(); string newVer = newNodeList.Item(i).Attributes["Ver"].Value.Trim(); ArrayList oldFileAl = new ArrayList(); for (int j = 0; j < oldNodeList.Count; j++) { string oldFileName = oldNodeList.Item(j).Attributes["Name"].Value.Trim(); string oldVer = oldNodeList.Item(j).Attributes["Ver"].Value.Trim(); oldFileAl.Add(oldFileName); oldFileAl.Add(oldVer); } int pos = oldFileAl.IndexOf(newFileName); if (pos == -1) { fileList[0] = newFileName; fileList[1] = newVer; updateFileList.Add(result, fileList); result++; } else if (pos > -1 && newVer.CompareTo(oldFileAl[pos + 1].ToString()) > 0) { fileList[0] = newFileName; fileList[1] = newVer; updateFileList.Add(result, fileList); result++; } } return(result); }
/// <summary> /// 检查更新文件,无参数 /// </summary> /// <returns></returns> public int CheckForUpdate() { string localXmlFile = Application.StartupPath + "\\UpdateList.xml"; if (!File.Exists(localXmlFile)) { return(-1); } XmlFiles updaterXmlFiles = new XmlFiles(localXmlFile); string tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\"; this._updaterURL = updaterXmlFiles.GetNodeValue("//Url") + "/UpdateList.xml"; this.DownAutoUpdateFile(tempUpdatePath); string serverXmlFile = tempUpdatePath + "\\UpdateList.xml"; if (!File.Exists(serverXmlFile)) { return(-1); } XmlFiles serverXmlFiles = new XmlFiles(serverXmlFile); XmlFiles localXmlFiles = new XmlFiles(localXmlFile); XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files"); XmlNodeList oldNodeList = localXmlFiles.GetNodeList("AutoUpdater/Files"); int result = 0; for (int i = 0; i < newNodeList.Count; i++) { string[] fileList = new string[3]; string newFileName = newNodeList.Item(i).Attributes["Name"].Value.Trim(); string newVer = newNodeList.Item(i).Attributes["Ver"].Value.Trim(); ArrayList oldFileAl = new ArrayList(); for (int j = 0; j < oldNodeList.Count; j++) { string oldFileName = oldNodeList.Item(j).Attributes["Name"].Value.Trim(); string oldVer = oldNodeList.Item(j).Attributes["Ver"].Value.Trim(); oldFileAl.Add(oldFileName); oldFileAl.Add(oldVer); } int pos = oldFileAl.IndexOf(newFileName); if (pos == -1) { fileList[0] = newFileName; fileList[1] = newVer; result++; } else if (pos > -1 && newVer.CompareTo(oldFileAl[pos + 1].ToString()) > 0) { fileList[0] = newFileName; fileList[1] = newVer; result++; } } return(result); }
/// <summary> /// AutoUpdate /// </summary> public void ForUpdate() { try { #region 自动更新 /*------------------自动更新----------------------------------*/ DrectSoft.DSSqlHelper.DS_SqlHelper.CreateSqlHelper(); string ip = ""; string sql = " select * from appcfg where configkey = 'ServiceIp' "; DataTable dt = DrectSoft.DSSqlHelper.DS_SqlHelper.ExecuteDataTable(sql, CommandType.Text); if (dt != null && dt.Rows.Count > 0) { ip = dt.Rows[0]["value"].ToString().Trim(); } if (!ISIp(ip)) { DrectSoft.Common.Ctrs.DLG.MyMessageBox.Show("配置的自动更新服务器IP地址不是有效的ip地址!"); return; } if (PublicClass.Ping(ip))//这里填写自动更新服务器IP { #region 自动启动AutoUpdate string localXmlFile = AppDomain.CurrentDomain.BaseDirectory + @"UpdateList.xml"; string serverXmlFile = string.Empty; try { //从本地读取更新配置文件信息 updaterXmlFiles = new XmlFiles(localXmlFile); } catch { MessageBox.Show("更新配置文件出错,暂时无法获取新版本!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //获取服务器地址 updateUrl = string.Format(updaterXmlFiles.GetNodeValue("//Url"), ip); updaterXmlFiles.GetElementsByTagName("Url")[0].InnerXml = updateUrl; AppUpdater appUpdater = new AppUpdater(); appUpdater.UpdaterURL = updateUrl + "UpdateList.xml"; //与服务器连接,下载更新配置文件。 try { tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\"; appUpdater.DownAutoUpdateFile(tempUpdatePath); } catch { MessageBox.Show("与服务器连接失败,操作超时!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); return; } //获取更新文件列表 Hashtable htUpdateFile = new Hashtable(); serverXmlFile = tempUpdatePath + "UpdateList.xml"; if (!File.Exists(serverXmlFile)) { MessageBox.Show("没有发现服务器更新文件列表!"); return; } availableUpdate = appUpdater.CheckForUpdate(serverXmlFile, localXmlFile, out htUpdateFile); if (availableUpdate > 0) { //this.Close(); //this.Hide(); System.Diagnostics.Process.Start(AppDomain.CurrentDomain.BaseDirectory + "AutoUpdate.exe", ip); Application.Exit(); System.Threading.Thread.Sleep(1000);//延时1秒 return; } #endregion } else { MessageBox.Show("无法Ping通服务器IP:" + ip + ",暂时无法判断软件版本更新!"); } /*----------------------------------------------------*/ #endregion labelControlWait.Visible = false; progressBarControlWait.Visible = false; LoadVersion(); } catch (Exception ex) { this.CustomMessageBox.MessageShow(ex.Message); m_FormMain.FormClosing -= new FormClosingEventHandler(m_FormMain.FormMain_FormClosing); Application.Exit(); } }