private void HandleFilesToUpdate(IAsyncResult res) { Action action = new Action(() => { try { DeleteAbateFiles(); var filesNeedDownload = new FilesNeedUpdate { Files = _files.Files.ToList().FindAll(o => !o.IsDelete).ToArray(), Directories = _files.Directories.ToList().FindAll(o => !o.IsDelete).ToArray() }; if (!filesNeedDownload.IsEmpty) { StartDownload(filesNeedDownload); } else { ReStartMainApp(); } _helper.SaveNewVersion(); } catch (Exception ex) { HandleException(ex); } }); action.BeginInvoke(null, null); }
public MainWindow(UpdateHelper helper) : this() { _mainApp = helper.SoftPath; _helper = helper; _files = helper.Files; this.Loaded += new RoutedEventHandler(MainWindow_Loaded); }
private void StartDownload(FilesNeedUpdate files) { //var section = _helper.UpdateSection; //if (section == null || string.IsNullOrEmpty(section.SoftKey)) //{ // ReStartMainApp(); //} _dispatcher.Invoke(new Action(() => { LoadingLabel.Text = "新版本文件远程压缩中……"; }));//DispatcherPriority.SystemIdle:先绘制完界面再执行这段逻辑 HttpClient httpClient = new HttpClient(); MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter(); HttpContent content = new ObjectContent <FilesNeedUpdate>(files, jsonFormatter); var response = httpClient.PostAsync(ConfigurationManager.AppSettings["VersionApiRoot"] + "CompressFilesNeedUpdate", content).Result; if (response.IsSuccessStatusCode) { _zipFileName = response.Content.ReadAsAsync <string>().Result; response = httpClient.GetAsync(ConfigurationManager.AppSettings["VersionApiRoot"] + "GetFilesUpdateUrl?softKey=" + _helper.UpdateSection.SoftKey).Result; if (response.IsSuccessStatusCode) { var url = response.Content.ReadAsAsync <string>().Result; if (!url.EndsWith("/")) { url += "/"; } url += _zipFileName; _dispatcher.Invoke(new Action(() => { //将压缩文件下载到临时文件夹 LoadingLabel.Text = "新版本文件下载中……"; })); _log.Debug(url); _client.DownloadFileAsync(new Uri(url), GetTempFolder() + "\\" + _zipFileName); } else { throw new HttpRequestException(response.ReasonPhrase); } } else { throw new HttpRequestException(response.ReasonPhrase); } }
/// <summary> /// 删除已过期的文件 /// </summary> private void DeleteAbateFiles() { var filesNeedDelete = new FilesNeedUpdate { Files = _files.Files.ToList().FindAll(o => o.IsDelete).ToArray(), Directories = _files.Directories.ToList().FindAll(o => o.IsDelete).ToArray() }; if (!filesNeedDelete.IsEmpty) { _dispatcher.Invoke(new Action(() => { CompleteLabel.Text = "正在删除已过期文件……"; }), DispatcherPriority.Normal); FilesHandler.DeleteFiles(filesNeedDelete.Files.Select(o => o.Name).ToArray(), filesNeedDelete.Directories.Select(o => o.Name).ToArray(), this.GetAppRootPath()); } }