public static List <FileENT> GetUpdateList() { List <FileENT> list = new List <FileENT>(); XmlDocument xml = new XmlDocument(); xml.Load(AppParameter.LocalUPdateConfig); XmlNodeList nodeList = xml.SelectNodes("/updateFiles/file[@version>" + AppParameter.Version + "]"); FileENT ent = null; foreach (XmlNode node in nodeList) { ent = new FileENT(); ent.FileFullName = node.Attributes["name"].Value; ent.Src = node.Attributes["src"].Value; ent.Version = node.Attributes["version"].Value; ent.Size = Convert.ToInt32(node.Attributes["size"].Value); ent.Option = (UpdateOption)Enum.Parse(typeof(UpdateOption), node.Attributes["option"].Value); list.Add(ent); } return(list); }
/// <summary> /// 执行单个更新 /// </summary> /// <param name="ent"></param> /// <returns></returns> public bool ExecUpdateItem(FileENT ent) { bool result = true; try { if (ent.Option == UpdateOption.del) { File.Delete(ent.FileFullName); } else { HttpHelper.DownLoadFile(ent.Src, Path.Combine(AppParameter.MainPath, ent.FileFullName)); } } catch { result = false; } return(result); }
/// <summary> /// 更新 /// </summary> public void UpdateApp() { //bool flag = false; int successCount = 0; int failCount = 0; int itemIndex = 0; List <FileENT> list = ConfigHelper.GetUpdateList(); if (list.Count == 0) { MessageBox.Show("版本已是最新", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.btnFinish.Enabled = true; this.btnStart.Enabled = false; isDelete = false; this.Close(); return; } //Thread[] arrThread = new Thread[5]; //for (int i = 0; i < arrThread.Length; i++) //{ // arrThread[i] = new Thread(new ThreadStart(delegate() // { thread = new Thread(new ThreadStart(delegate { #region thread method FileENT ent = null; while (true) { lock (this) { if (itemIndex >= list.Count) { break; } ent = list[itemIndex]; //PrintResultDelegate pd = PrintResult; string msg = string.Empty; if (ExecUpdateItem(ent)) { msg = ent.FileFullName + "更新成功"; successCount++; } else { msg = ent.FileFullName + "更新失败"; failCount++; } if (this.InvokeRequired) { //this.Invoke(pd, msg, // (int)Math.Ceiling(1f / list.Count * 100)); this.Invoke((Action) delegate() { listBox1.Items.Add(msg); int val = (int)Math.Ceiling(1f / list.Count * 100); progressBar1.Value = progressBar1.Value + val > 100 ? 100 : progressBar1.Value + val; }); } itemIndex++; if (successCount + failCount == list.Count && this.InvokeRequired) { string finishMessage = string.Empty; //SetButtonDelegate sbtn = SetButton; //this.Invoke(sbtn); if (this.InvokeRequired) { this.Invoke((Action) delegate() { btnFinish.Enabled = true; }); } isDelete = failCount != 0; if (!isDelete) { AppParameter.Version = list.Last().Version; ConfigHelper.UpdateAppConfig("version", AppParameter.Version); finishMessage = "升级完成,程序已成功升级到" + AppParameter.Version; } else { finishMessage = "升级完成,但不成功"; } MessageBox.Show(finishMessage, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); runningLock = false; } } } #endregion })); // })); //} runningLock = true; thread.Start(); //foreach (Thread t in arrThread) //{ // t.Start(); //} }