private void Form1_Load(object sender, EventArgs e) { ftpaddress = ConfigurationManager.AppSettings["FtpAddress"]; ftpaccount = ConfigurationManager.AppSettings["FtpAccount"]; ftppassword = ConfigurationManager.AppSettings["FtpPassword"]; updateFile = ConfigurationManager.AppSettings["UpdateFile"]; updateProcess = ConfigurationManager.AppSettings["UpdateProcess"]; updateProcessPath = ConfigurationManager.AppSettings["UpdateProcessPath"]; isStartAfterUpdate = ConfigurationManager.AppSettings["IsStartAfterUpdate"]; string path = AppDomain.CurrentDomain.BaseDirectory; if (!Directory.Exists(path + "BackUp")) { Directory.CreateDirectory(path + "BackUp"); } //MessageBox.Show(path); verModle = ReadConfig(path + "UpdateVersion.xml"); string passive = ConfigurationManager.AppSettings["Passive"]; //下载更新文件 ftp = new FTPHelper(ftpaddress, "", ftpaccount, ftppassword, passive == "1"?true:false); bool ret = ftp.Download(path, updateFile); if (ret) { listBoxTip.Items.Clear(); //开始解析文件,查看哪些文件需要更新 arrUpdate = ParseVersionFile(path + updateFile); foreach (var item in arrUpdate) { listBoxTip.Items.Add(item); } progressBar1.Maximum = arrUpdate.Count; if (arrUpdate.Count > 0) { DialogResult dr = MessageBox.Show("有更新文件,确认更新?", "提示", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) { if (KillProcess()) { Thread.Sleep(2000); Thread td = new Thread(WorkThread); td.Start(); } else { MessageBox.Show("请关闭更新进程,然后手动执行更新程序"); } } else { this.Close(); } } else { if (File.Exists(path + updateFile)) { File.Delete(path + updateFile); } //File.Move(path + updateFile, path + "UpdateVersion.xml"); if (args != null && args.Length > 0 && args[0] == "1") { MessageBox.Show("项目工程没有更新!"); } this.Close(); } } else { MessageBox.Show("下载文件出错" + updateFile); } }
private void WorkThread() { try { string root1 = AppDomain.CurrentDomain.BaseDirectory; DirectoryInfo di1 = new DirectoryInfo(root1); DirectoryInfo di2 = di1.Parent.Parent; string root = di2.FullName; //Directory.SetCurrentDirectory(root); bool update = false; int i = 0; foreach (var item in arrUpdate) { //判断是否是目录 string ext = Path.GetExtension(item.ToString()); if (string.IsNullOrEmpty(ext)) { if (!Directory.Exists(root + item.ToString())) { Directory.CreateDirectory(root + item.ToString()); this.Invoke((EventHandler) delegate { listBoxTip.Items.Add(item.ToString()); }); } } else { string path = Path.GetDirectoryName(item.ToString()) + "\\"; string name = Path.GetFileName(item.ToString()); if (File.Exists(path + item.ToString())) { File.Delete(path + item.ToString()); } bool ret = ftp.Download(root, item.ToString()); if (ret) { this.Invoke((EventHandler) delegate { listBoxTip.Items.Add(item.ToString()); }); } else { LogClass.WriteLogFile("download file error:" + item.ToString()); } } i++; this.Invoke((EventHandler) delegate { progressBar1.Value = i; }); listBoxTip.TopIndex = listBoxTip.Items.Count - 1; } this.Invoke((EventHandler) delegate { MessageBox.Show("更新完成"); if (File.Exists(root1 + "UpdateVersion.xml")) { File.Delete(root1 + "UpdateVersion.xml"); } Thread.Sleep(100); File.Move(root1 + updateFile, root1 + "UpdateVersion.xml"); if (File.Exists(root1 + "BackUp/UpdateVersion.xml")) { File.Delete(root1 + "BackUp/UpdateVersion.xml"); } File.Copy(root1 + "UpdateVersion.xml", root1 + "BackUp/UpdateVersion.xml"); if (isStartAfterUpdate == "1") { Process.Start(root1 + updateProcessPath); } this.Close(); }); } catch (Exception e) { LogClass.WriteLogFile("thread error:" + e.Message); } }