/// <summary> /// 判断当前升级包是否准备好 /// </summary> /// <returns></returns> public bool UpgradeProjectIsValid() { if (String.IsNullOrEmpty(txtStartUpApp.Text)) { XtraMessageBox.Show("未设置升级完成后的启动主程序名字。"); txtStartUpApp.Focus(); return(false); } if (String.IsNullOrEmpty(txtUpgradeUrl.Text)) { XtraMessageBox.Show("未设置保存升级项目的服务器地址。"); txtUpgradeUrl.Focus(); return(false); } if (String.IsNullOrEmpty(txtProduct.Text)) { XtraMessageBox.Show("未设置本次升级项目的软件名称。"); txtProduct.Focus(); return(false); } if (String.IsNullOrEmpty(txtVersion.Text)) { XtraMessageBox.Show("未设置本次升级的软件版本号。"); txtVersion.Focus(); return(false); } if (lvGroups.Items.Count < 1) { XtraMessageBox.Show("没有为本次升级添加相关的升级组。"); return(false); } UpgradeProject proj = Presenter.UpgradeService.GetUpgradeProject(txtProduct.Text); if (proj != null) { Version newVer = new Version(txtVersion.Text); Version oldVer = new Version(proj.Version); if (newVer == oldVer) { if (XtraMessageBox.Show("服务器已经存在版本为\"" + proj.Version + "\"的升级项目,您依然要上传本次升级项目吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.No) { return(false); } } if (newVer < oldVer) { XtraMessageBox.Show("本次升级的版本号比服务器端的版本低客户端将得不到更新。"); return(false); } } return(true); }
/// <summary> /// 重启程序 /// </summary> /// <param name="project"></param> private void ReStartApplication(UpgradeProject project) { string upgradeLaunch = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), UPGRADELAUNCH_FILE); if (File.Exists(upgradeLaunch)) { string upgradePath = FileUtility.ConvertToFullPath(@"..\Upgrade\") + project.Product + @"\" + project.Version; Process.Start(upgradeLaunch, upgradePath); Environment.Exit(0); } }
/// <summary> /// 系统更新侦测程序 /// </summary> private void StartDetectUpgrade() { while (IsRun) { Thread.Sleep(checkInterval * 1000 * 60); UpgradeProject proj = liveupgradeService.GetValidUpgradeProject(); if (proj != null) { liveupgradeService.UpgradeNotify(proj); } } }
/// <summary> /// 选择升级项目的入口程序 /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="DevExpress.XtraEditors.Controls.ButtonPressedEventArgs"/> instance containing the event data.</param> private void txtStartUpApp_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e) { OpenFileDialog opendlg = new OpenFileDialog(); opendlg.Multiselect = false; opendlg.Title = "选择"; opendlg.Filter = "可执行程序(*.exe)|*.exe"; if (opendlg.ShowDialog() == DialogResult.OK) { txtStartUpApp.Text = Path.GetFileName(opendlg.FileName); txtProduct.Text = Path.GetFileNameWithoutExtension(opendlg.FileName); // 向升级项目中插入启动组的内容 UpgradeGroup group = new UpgradeGroup("StartUp", "${RootPath}"); group.Items.Add(new UpgradeItem(opendlg.FileName)); foreach (ListViewItem item in lvGroups.Items) { UpgradeGroup gr = item.Tag as UpgradeGroup; if (gr != null && gr.Name == "StartUp") { lvGroups.Items.Remove(item); break; } } ListViewItem groupItem = new ListViewItem(group.Name); groupItem.Tag = group; lvGroups.Items.Insert(0, groupItem); BindingUpgradeGroup(group); try { AssemblyName assemblyName = Assembly.ReflectionOnlyLoadFrom(opendlg.FileName).GetName(); txtVersion.Text = assemblyName.Version.ToString(); } catch { UpgradeProject proj = Presenter.UpgradeService.GetUpgradeProject(txtProduct.Text.Trim()); // 从服务器升级数据库中获取新的版本号 if (proj != null) { Version ver = new Version(proj.Version); txtVersion.Text = String.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision + 1); } else { txtVersion.Text = "1.0.0.0"; } } } txtUpgradeUrl.Text = Presenter.UpgradeSetting.UpgradeUrl; txtUpgradeUrl.SelectAll(); txtUpgradeUrl.Focus(); }
/// <summary> /// 保存更新配置文件 /// </summary> /// <param name="project">更新项目</param> private void SerializeUpgrade(UpgradeProject project) { string upgradeConfigfile = FileUtility.ConvertToFullPath(@"..\Upgrade\") + project.Product + @"\" + project.Version + @"\Upgrade.dat"; Serializer serializer = new Serializer(); try { byte[] buffer = serializer.Serialize <UpgradeProject>(project); FileStream fs = new FileStream(upgradeConfigfile, FileMode.Create); fs.Write(buffer, 0, buffer.Length); fs.Close(); } catch { } }
public void OnCheckUpgrade(object sender, EventArgs e) { ILiveUpgradeService upgradeService = WorkItem.Services.Get <ILiveUpgradeService>(); if (upgradeService != null) { using (WaitCursor cursor = new WaitCursor(true)) { UpgradeProject proj = upgradeService.GetValidUpgradeProject(); if (proj != null) { upgradeService.UpgradeNotify(proj); } } } }
/// <summary> /// 系统升级提示 /// </summary> /// <param name="project"></param> public void UpgradeNotify(UpgradeProject project) { string upgradeUrl = UpgradeService.GetUpgradeUrl(project.Product, project.Version); if (XtraMessageBox.Show(String.Format("服务器发布了 \'{0}\" 的新版本 {1},您要立即升级到新版本吗?", project.Product, project.Version), "升级提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { UpgradeProcess up = new UpgradeProcess(project, upgradeUrl); if (up.ShowDialog() == DialogResult.OK) { SerializeUpgrade(project); ReStartApplication(project); } } }
public UpgradeProcess(UpgradeProject upgradeProject, string upgradeUrl) { this.upgradeProject = upgradeProject; this.upgradeUrl = upgradeUrl; this.upgradeTempPath = FileUtility.ConvertToFullPath(@"..\Upgrade\") + upgradeProject.Product + @"\" + upgradeProject.Version + @"\"; if (!Directory.Exists(upgradeTempPath)) { Directory.CreateDirectory(upgradeTempPath); } downloadItems.Clear(); foreach (UpgradeGroup group in upgradeProject.Groups) { downloadItems.AddRange(group.Items); } InitializeComponent(); }
public UpgradeProject WrapUpgradeProject() { UpgradeProject proj = new UpgradeProject(); proj.StartUpApp = txtStartUpApp.Text.Trim(); proj.UpgradeServer = txtUpgradeUrl.Text.Trim(); proj.Product = txtProduct.Text.Trim(); proj.Version = txtVersion.Text.Trim(); proj.Description = txtDescription.Text; foreach (ListViewItem item in lvGroups.Items) { UpgradeGroup group = item.Tag as UpgradeGroup; if (group != null) { proj.Groups.Add(group); } } return(proj); }
/// <summary> /// 上传升级项目到更新服务器 /// </summary> public void UploadProject() { if (!View.UpgradeProjectIsValid()) { return; } try { using (WaitCursor cursor = new WaitCursor(true)) { project = View.WrapUpgradeProject(); uploadVirtaulPath = UpgradeService.CreateVirtualDirectory(project.UpgradeServer, project); List <UpgradeItem> items = new List <UpgradeItem>(); foreach (UpgradeGroup group in project.Groups) { items.AddRange(group.Items); } uploadTotal = 0; WebClient client = new WebClient(); client.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged); foreach (UpgradeItem item in items) { Uri uri = new Uri(uploadVirtaulPath + "/" + item.Name); client.UploadFile(uri, "PUT", item.FileName); uploadTotal += item.Size; } UpgradeService.CreateUpgradeProject(project); } } catch (Exception ex) { XtraMessageBox.Show("上传系统升级项目失败, " + ex.Message); } finally { OnShellProgressBarChanged(-1); } }
public LaunchProgress(string upgradePath) : this() { this.upgradePath = upgradePath; string configfile = Path.Combine(upgradePath, UPGRADE_CONFIGFILE); if (!File.Exists(configfile)) { throw new Exception(String.Format("不存在升级配置文件 {0}。", configfile)); } // 加载日志组件 XmlConfigurator.Configure(new FileInfo(UPGRADE_LOGCONFIGFILE)); logger = LogManager.GetLogger(typeof(LaunchProgress)); // 反序列化升级配置文件 FileStream fs = new FileStream(configfile, FileMode.Open); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (int)fs.Length); Serializer serializer = new Serializer(); project = serializer.Deserialize <UpgradeProject>(buffer); }
public UpgradeProject GetValidUpgradeProject() { UpgradeElements upgradeElements = null; LiveUpgradeConfigurationSection cs = ConfigurationManager.GetSection(LIVEUPGRADE_SECTION) as LiveUpgradeConfigurationSection; if (cs != null) { upgradeElements = cs.UpgradeProducts; } // 检查需要更新的升级项目 // 先检查相对于本地版本号大1的版本如果没有的话则直接获取其服务器端最新的更新版本 foreach (UpgradeElement element in upgradeElements) { Version ver = new Version(element.LocalVersion); string upgradeVersion = String.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision + 1); UpgradeProject proj = UpgradeService.GetUpgradeProject(element.Product, upgradeVersion); if (proj == null) { proj = UpgradeService.GetUpgradeProject(element.Product); if (proj == null) { continue; } } Version localVer = new Version(element.LocalVersion); Version newVer = new Version(proj.Version); if (localVer < newVer || proj.UpgradePatchTime > element.UpgradeDate) { return(proj); } } return(null); }