public override async Task <UpdateResult> DownloadAndInstall(IProgress <double> progress, CheckForUpdatesResult?check = null) { Logger.LogInformation("Download and install"); try { Logger.LogInformation("Source: {0}", Current.GetAppInstallerInfo().Uri); var pm = new PackageManager(); var downloadTask = pm.AddPackageByAppInstallerFileAsync(Current.GetAppInstallerInfo().Uri, AddPackageByAppInstallerOptions.ForceTargetAppShutdown, pm.GetDefaultPackageVolume()); downloadTask.Progress = (info, prog) => progress?.Report(prog.percentage / 100d); var result = await downloadTask.AsTask(); if (result.IsRegistered) { return new UpdateResult { Result = true } } ; else { return new UpdateResult { Result = false, ErrorCode = result.ExtendedErrorCode.HResult, ErrorMessage = result.ErrorText } }; } catch (Exception e) { Crashes.TrackError(e); Logger.LogError("Thrown exception: {0}\n{1}", e.Message, e.StackTrace); return(new UpdateResult { Result = false, ErrorCode = e.HResult, ErrorMessage = Platform.GetLocalizedString("/Shared/Updater/UpdateError") }); } }
private async void Button_Click(object sender, RoutedEventArgs e) { try { PackageManager manager = new PackageManager(); var test = manager.FindPackageForUser(string.Empty, Package.Current.Id.FullName); var updateAvailable = await test.CheckUpdateAvailabilityAsync(); var info = test.GetAppInstallerInfo(); var dialog = new MessageDialog($"Update ? : {updateAvailable.Availability}, Uri = {info?.Uri}"); await dialog.ShowAsync(); if (updateAvailable.Availability == PackageUpdateAvailability.Available || updateAvailable.Availability == PackageUpdateAvailability.Required) { //Try to update the app dialog = new MessageDialog($"Starting update"); await dialog.ShowAsync(); var result = await manager.AddPackageByAppInstallerFileAsync(info.Uri, AddPackageByAppInstallerOptions.ForceTargetAppShutdown, manager.GetDefaultPackageVolume()); //var result = await manager.UpdatePackageAsync(info.Uri, null, DeploymentOptions.ForceApplicationShutdown); dialog = new MessageDialog($"Finished update ´: {result.ErrorText}, {result.ExtendedErrorCode}"); await dialog.ShowAsync(); if (!string.IsNullOrEmpty(result.ErrorText)) { dialog = new MessageDialog($"Error: {result.ExtendedErrorCode}, {result.ErrorText}"); await dialog.ShowAsync(); } else { AppRestartFailureReason restartResult = await CoreApplication.RequestRestartAsync(string.Empty); if (restartResult == AppRestartFailureReason.NotInForeground || restartResult == AppRestartFailureReason.RestartPending || restartResult == AppRestartFailureReason.Other) { dialog = new MessageDialog($"Restart: {restartResult}"); await dialog.ShowAsync(); } } } if (updateAvailable.Availability == PackageUpdateAvailability.Error) { dialog = new MessageDialog($"Error: {updateAvailable.ExtendedError}"); await dialog.ShowAsync(); } } catch (Exception ex) { var dialog = new MessageDialog($"exception: {ex}, InnerException {ex.InnerException}"); await dialog.ShowAsync(); } }
private async void Install_Click(object sender, RoutedEventArgs e) { Buttons.Visibility = Visibility.Collapsed; TitleText.Visibility = Progress.Visibility = Visibility.Visible; Progress.IsIndeterminate = true; if (!CertFound) { var result = await LaunchAdmin("--install-cert"); if (result != 0) { TitleText.Visibility = Progress.Visibility = Visibility.Collapsed; Progress.IsIndeterminate = false; } switch (result) { case 1: Error.Text = "An invalid certificate has been detected"; return; case -99: Error.Text = "Admin permissions are required for certificate installation"; return; } } bool done = await Task.Run(async() => { try { var installTask = pm.AddPackageByAppInstallerFileAsync(new Uri(Variables.AppInstallerUrl), AddPackageByAppInstallerOptions.ForceTargetAppShutdown, pm.GetDefaultPackageVolume()); Dispatcher.Invoke(() => { Progress.IsIndeterminate = false; TaskbarProgress.ProgressState = TaskbarItemProgressState.Normal; }); installTask.Progress = (asyncInfo, prog) => Dispatcher.Invoke(() => TaskbarProgress.ProgressValue = Progress.Value = prog.percentage / 100d); var result = await installTask.AsTask(); Dispatcher.Invoke(() => { Progress.Visibility = Visibility.Collapsed; TaskbarProgress.ProgressState = TaskbarItemProgressState.Normal; }); if (result.IsRegistered) { return(true); } else { Dispatcher.Invoke(() => { TitleText.Visibility = Visibility.Collapsed; Error.Text = result.ErrorText; }); } return(false); } catch (Exception i) { Dispatcher.Invoke(() => { if (i.HResult == -2147009281) { TitleText.Visibility = Visibility.Collapsed; Progress.Visibility = Visibility.Collapsed; Error.Text = "Enable \"Sideload apps\" and rerun the installer"; OpenSettings.Visibility = Visibility.Visible; } else { TitleText.Visibility = Visibility.Collapsed; Progress.Visibility = Visibility.Collapsed; Error.Text = string.Format("Unable to install app. Error: 0x{0:X}", i.HResult); } }); return(false); } }); if (done) { var pkg = pm.FindPackagesForUser(string.Empty, Variables.PackageFamilyName).FirstOrDefault(); var app = (await pkg.GetAppListEntriesAsync()).FirstOrDefault(); await app.LaunchAsync(); Close(); } }