public UpdateWindowViewModel(string directoryName, IUpgrade upgrade, string startApp = "", Action success = null, Action failed = null, Action cancel = null ) { this.directoryName = directoryName; this.upgrade = upgrade; this.startApp = startApp; this.success = success; this.failed = failed; this.cancel = cancel; UpdateCollection = new UpdateFileCollection(new List<EsuUpgradeInfo>()); updateCommand = new DelegateCommand(Update, () => true); closeCommand = new DelegateCommand(Close, () => true); cancelCommand = new DelegateCommand(Cancel, () => true); scheduler = TaskScheduler.Current; }
public async void Update() { if (upgrade == null) return; Message = "正在获取升级数据 ..."; CanUpdate = false; tokenSource = new CancellationTokenSource(); await ThreadHelper.StartTask(() => { EsuUpgradeInfoCollection serviceList = upgrade.GetServiceFileCollection(); string tempPath = string.Format("{0}\\UpdateTemp", Environment.CurrentDirectory); if (!Directory.Exists(tempPath)) Directory.CreateDirectory(tempPath); UpdateCollection = new UpdateFileCollection(serviceList.GetDifferent(upgrade.GetLocalFileCollection())); if (UpdateCollection.Count == 0) { MessageBox.Show("未发现可更新的组件."); return; } Message = "获取数据完成,正在准备升级 ..."; #region download files to local path foreach (UpdateFile updateFile in UpdateCollection.Where(w => w.FileInfo.Type == FileType.Directory)) { Message = string.Format("正在创建文件夹{0} ...", updateFile.FileInfo.Name); CurrentUpdate = updateFile; Directory.CreateDirectory(string.Format(@"{0}\{1}", tempPath, updateFile.FileInfo.RelativeFileName)); updateFile.Upgradable = true; Progress.StepAdd(); } foreach (UpdateFile updateFile in UpdateCollection.Where(w => w.FileInfo.Type == FileType.File)) { Message = string.Format("正在下载文件{0} ...", updateFile.FileInfo.Name); CurrentUpdate = updateFile; string source = string.Format(@"{0}\{1}", tempPath, updateFile.FileInfo.RelativeFileName); //string target = string.Format(@"{0}\{1}", Environment.CurrentDirectory, updateFile.FileInfo.RelativeFileName); upgrade.GetFileBytes(updateFile.FileInfo.RelativeFileName) .ByteToFile(source); updateFile.Upgradable = true; Thread.Sleep(100); Progress.StepAdd(10); } }); #endregion Message = "组件下载完成,正在启动更新组件 ..."; if (success != null) success(); try { string copyApplicationFileName = string.Format("{0}\\UpdateToolkit.exe", Environment.CurrentDirectory); if (File.Exists(copyApplicationFileName)) Process.Start(copyApplicationFileName, startApp); else MessageBox.Show("未发现更新组件,请联系系统管理员。"); Environment.Exit(0); } catch { MessageBox.Show("复制组件发生错误,请联系系统管理员。"); } }