private VersionModel ReadConfig(string filepath) { VersionModel version = new VersionModel(); XmlDocument xd = new XmlDocument(); string fileName = filepath; try { xd.Load(fileName); version.LastTime = xd.SelectSingleNode("//LastUpdateTime").InnerText; version.Version = xd.SelectSingleNode("//Version").InnerText; version.PubNumber = xd.SelectSingleNode("//PubNumber").InnerText; XmlNodeList FileList = xd.SelectNodes("//UpdateFileList/File"); foreach (XmlNode xn in FileList) { string ver = xn.Attributes["Ver"].Value; string lasttime = xn.Attributes["LastTime"].Value; string name = xn.Attributes["Name"].Value; string pubnumber = xn.Attributes["PubNumber"].Value; FileDetails file = new FileDetails(); file.lastime = lasttime; file.version = ver; file.pubnumber = pubnumber; version.fileDic[name] = file; } } catch (Exception e) { LogClass.WriteLogFile("readconfig error:" + e.Message); } return(version); }
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 ArrayList ParseVersionFile(string file) { ArrayList arr = new ArrayList(); string root = AppDomain.CurrentDomain.BaseDirectory; //string root = Environment.CurrentDirectory.ToString(); DirectoryInfo di1 = new DirectoryInfo(root); DirectoryInfo di2 = di1.Parent.Parent; root = di2.FullName; try { verStdModle = ReadConfig(file); //判断文件主版本是否已经改变,需要更新 if (verStdModle.Version != verModle.Version) { foreach (var item in verStdModle.fileDic) { if (verStdModle.PubNumber == item.Value.pubnumber) { if (verModle.fileDic.ContainsKey(item.Key.ToString())) { if (verModle.fileDic[item.Key.ToString()].version != item.Value.version.ToString()) { arr.Add(item.Key.ToString()); } } else { arr.Add(item.Key.ToString()); } } else { //发布计数不相等,需要删除这个文件 string ext = Path.GetExtension(root + item.Key.ToString()); if (string.IsNullOrEmpty(ext)) { if (Directory.Exists(root + item.Key.ToString())) { DirectoryInfo di = new DirectoryInfo(root + item.Key.ToString()); if (di.GetFiles().Length > 0) { //di.Delete(true); } else { //Directory.Delete(root + item.Key.ToString()); } } } else { if (File.Exists(root + item.Key.ToString())) { //File.Delete(root + item.Key.ToString()); } } } } } } catch (Exception e) { LogClass.WriteLogFile("ParseVersionFile error :" + e.Message); } return(arr); }