/// <summary> /// 检查更新文件 /// </summary> /// <param name="serverXmlFile"></param> /// <param name="localXmlFile"></param> /// <param name="updateFileList"></param> /// <returns></returns> internal int CheckForUpdate(out Hashtable updateFileList) { updateFileList = new Hashtable(); string localXmlFile = Application.StartupPath + "\\" + UpdateConfigName; //加载本地升级配置文件 XmlFiles updaterXmlFiles; try { updaterXmlFiles = new XmlFiles(localXmlFile); } catch { throw new Exception("配置文件出错"); } //与服务器连接,下载更新配置文件 string downLoadPath = string.Empty; try { downLoadPath = tempUpdatePath; this.DownAutoUpdateFile(tempUpdatePath); } catch { throw new Exception("与服务器连接失败"); } string serverXmlFile = Path.Combine(downLoadPath, UpdateConfigName); XmlFiles serverXmlFiles = new XmlFiles(serverXmlFile); XmlFiles localXmlFiles = new XmlFiles(localXmlFile); XmlNodeList newNodeList = serverXmlFiles.GetNodeList("AutoUpdater/Files"); XmlNodeList oldNodeList = localXmlFiles.GetNodeList("AutoUpdater/Files"); int k = 0; for (int i = 0; i < newNodeList.Count; i++) { UpdateFile upateFile = new UpdateFile(); string newFileName = newNodeList.Item(i).Attributes["FileName"].Value.Trim(); string newVer = newNodeList.Item(i).Attributes["Version"].Value.Trim(); string newSavePath = newNodeList.Item(i).Attributes["SavePath"].Value.Trim(); string newUpdatePath = newNodeList.Item(i).Attributes["UpdatePath"].Value.Trim(); long newFileSize = Convert.ToInt64(newNodeList.Item(i).Attributes["FileSize"].Value.Trim()); ArrayList oldFileAl = new ArrayList(); for (int j = 0; j < oldNodeList.Count; j++) { string oldFileName = oldNodeList.Item(j).Attributes["FileName"].Value.Trim(); string oldVer = oldNodeList.Item(j).Attributes["Version"].Value.Trim(); oldFileAl.Add(oldFileName); oldFileAl.Add(oldVer); } int pos = oldFileAl.IndexOf(newFileName); if (pos == -1) { upateFile.FileName = newFileName; upateFile.Version = newVer; //upateFile.SavePath = newSavePath; //upateFile.UpdatePath = newUpdatePath; upateFile.FileSize = newFileSize; updateFileList.Add(k, upateFile); k++; } else if (pos > -1 && newVer.CompareTo(oldFileAl[pos + 1].ToString()) > 0) { upateFile.FileName = newFileName; upateFile.Version = newVer; //upateFile.SavePath = newSavePath; //upateFile.UpdatePath = newUpdatePath; upateFile.FileSize = newFileSize; updateFileList.Add(k, upateFile); k++; } } return k; }
public void Start() { try { string configName = Path.GetFullPath(UpdateConfigName); XmlFiles updaterXmlFiles; try { updaterXmlFiles = new XmlFiles(configName); } catch { throw new Exception("配置文件出错"); } UpdateUrl = updaterXmlFiles.GetNodeValue("//Url"); EntryPoint = updaterXmlFiles.GetNodeValue("//EntryPoint"); ApplicationId = updaterXmlFiles.FindNode("//Application").Attributes["applicationId"].Value; tempUpdatePath = Environment.GetEnvironmentVariable("Temp") + "\\" + "_" + ApplicationId + "_" + "y" + "_" + "x" + "_" + "m" + "_" + "\\"; mainAppExe = Path.GetFullPath(updaterXmlFiles.GetNodeValue("//Location")); OnPreUpdate(); //获取更新文件列表 Hashtable htUpdateFile = new Hashtable(); int availableUpdate = this.CheckForUpdate(out htUpdateFile); if (availableUpdate > 0) { List<UpdateFile> list = new List<UpdateFile>(); for (int i = 0; i < htUpdateFile.Count; i++) { list.Add((UpdateFile)htUpdateFile[i]); } OnGetUpdateFiles(list); Thread threadDown = new Thread(new ParameterizedThreadStart(DownUpdateFile)); threadDown.IsBackground = true; threadDown.Start(list); } else { OnNoUpdatesFound(); } } catch (Exception ex) { this.OnUpdateError(ex.Message); } }