public Task Add(string filePath, AddAppxPackageOptions options = 0, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default) { var proxyObject = new AddDto { FilePath = filePath, AllUsers = options.HasFlag(AddAppxPackageOptions.AllUsers), AllowDowngrade = options.HasFlag(AddAppxPackageOptions.AllowDowngrade), KillRunningApps = options.HasFlag(AddAppxPackageOptions.KillRunningApps) }; return(this.client.Invoke(proxyObject, cancellationToken, progress)); }
public override Task Invoke(IProxyObject command, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = null) { switch (command) { case AddDto addDto: { AddAppxPackageOptions addDtoOptions = 0; if (addDto.AllUsers) { addDtoOptions |= AddAppxPackageOptions.AllUsers; } if (addDto.AllowDowngrade) { addDtoOptions |= AddAppxPackageOptions.AllowDowngrade; } if (addDto.KillRunningApps) { addDtoOptions |= AddAppxPackageOptions.KillRunningApps; } return(this.SelfElevationAwareObject.Add(addDto.FilePath, addDtoOptions, cancellationToken, progress)); } case DeprovisionDto deprovisionDto: { return(this.SelfElevationAwareObject.Deprovision(deprovisionDto.PackageFamilyName, cancellationToken, progress)); } case RemoveDto removeDto: { return(this.SelfElevationAwareObject.Remove(removeDto.Packages, removeDto.Context == PackageContext.AllUsers, !removeDto.RemoveAppData, cancellationToken, progress)); } case RemoveCurrentUserDto removeCurrentUserDto: { return(this.SelfElevationAwareObject.Remove(removeCurrentUserDto.PackageFullNames, !removeCurrentUserDto.RemoveAppData, cancellationToken, progress)); } default: { throw new NotSupportedException(); } } }
public override Task Invoke(IProxyObject command, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = null) { if (command is AddDto addDto) { AddAppxPackageOptions addDtoOptions = 0; if (addDto.AllUsers) { addDtoOptions |= AddAppxPackageOptions.AllUsers; } if (addDto.AllowDowngrade) { addDtoOptions |= AddAppxPackageOptions.AllowDowngrade; } if (addDto.KillRunningApps) { addDtoOptions |= AddAppxPackageOptions.KillRunningApps; } return(this.SelfElevationAwareObject.Add(addDto.FilePath, addDtoOptions, cancellationToken, progress)); } if (command is DeprovisionDto deprovisionDto) { return(this.SelfElevationAwareObject.Deprovision(deprovisionDto.PackageFamilyName, cancellationToken, progress)); } if (command is CheckUpdateAvailabilityDto checkUpdatesDto) { return(this.SelfElevationAwareObject.CheckForUpdates(checkUpdatesDto.PackageFullName, cancellationToken, progress)); } if (command is StopDto stopDto) { return(this.SelfElevationAwareObject.Stop(stopDto.PackageFullName, cancellationToken)); } if (command is RemoveDto removeDto) { return(this.SelfElevationAwareObject.Remove(removeDto.Packages, removeDto.Context == PackageContext.AllUsers, !removeDto.RemoveAppData, cancellationToken, progress)); } if (command is RemoveCurrentUserDto removeCurrentUserDto) { return(this.SelfElevationAwareObject.Remove(removeCurrentUserDto.PackageFullNames, !removeCurrentUserDto.RemoveAppData, cancellationToken, progress)); } if (command is RunDto runPackageDto) { return(this.SelfElevationAwareObject.Run(runPackageDto.ManifestPath, runPackageDto.ApplicationId, cancellationToken, progress)); } if (command is RunToolInContextDto runToolInContextDto) { return(this.SelfElevationAwareObject.RunToolInContext(runToolInContextDto.PackageFamilyName, runToolInContextDto.AppId, runToolInContextDto.ToolPath, runToolInContextDto.Arguments, cancellationToken, progress)); } throw new NotSupportedException(); }
public async Task Add(string filePath, AddAppxPackageOptions options = 0, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default) { this.SideloadingConfigurator.AssertSideloadingEnabled(); Logger.Info("Installing package {0}", filePath); if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (string.Equals(Path.GetFileName(filePath), FileConstants.AppxManifestFile, StringComparison.OrdinalIgnoreCase)) { if (options.HasFlag(AddAppxPackageOptions.AllBundleResources)) { throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options)); } var reader = await AppxManifestSummaryReader.FromManifest(filePath).ConfigureAwait(false); DeploymentOptions deploymentOptions = 0; if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion; } if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= DeploymentOptions.ForceApplicationShutdown; deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown; } deploymentOptions |= DeploymentOptions.DevelopmentMode; await AsyncOperationHelper.ConvertToTask( PackageManagerWrapper.Instance.RegisterPackageAsync(new Uri(filePath), Enumerable.Empty <Uri>(), deploymentOptions), $"Installing {reader.DisplayName} {reader.Version}...", cancellationToken, progress).ConfigureAwait(false); } else if (string.Equals(FileConstants.AppInstallerExtension, Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase)) { if (options.HasFlag(AddAppxPackageOptions.AllUsers)) { throw new ArgumentException("Cannot install a package from .appinstaller for all users.", nameof(options)); } if (options.HasFlag(AddAppxPackageOptions.AllBundleResources)) { throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options)); } if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { throw new ArgumentException("Cannot force a downgrade with .appinstaller. The .appinstaller defines on its own whether the downgrade is allowed.", nameof(options)); } AddPackageByAppInstallerOptions deploymentOptions = 0; if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= AddPackageByAppInstallerOptions.ForceTargetAppShutdown; } var volume = PackageManagerWrapper.Instance.GetDefaultPackageVolume(); await AsyncOperationHelper.ConvertToTask( PackageManagerWrapper.Instance.AddPackageByAppInstallerFileAsync(new Uri(filePath, UriKind.Absolute), deploymentOptions, volume), "Installing from " + Path.GetFileName(filePath) + "...", cancellationToken, progress).ConfigureAwait(false); } else { string name, version, publisher; DeploymentOptions deploymentOptions = 0; switch (Path.GetExtension(filePath)) { case FileConstants.AppxBundleExtension: case FileConstants.MsixBundleExtension: { IAppxIdentityReader reader = new AppxIdentityReader(); var identity = await reader.GetIdentity(filePath, cancellationToken).ConfigureAwait(false); name = identity.Name; publisher = identity.Publisher; version = identity.Version; if (options.HasFlag(AddAppxPackageOptions.AllBundleResources)) { deploymentOptions |= DeploymentOptions.InstallAllResources; } break; } default: { if (options.HasFlag(AddAppxPackageOptions.AllBundleResources)) { throw new ArgumentException("Cannot use the flag AllBundleResources with non-bundle packages.", nameof(options)); } var reader = await AppxManifestSummaryReader.FromMsix(filePath).ConfigureAwait(false); name = reader.DisplayName; version = reader.Version; publisher = reader.Publisher; break; } } if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion; } if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= DeploymentOptions.ForceApplicationShutdown; deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown; } if (options.HasFlag(AddAppxPackageOptions.AllUsers)) { var deploymentResult = await AsyncOperationHelper.ConvertToTask( PackageManagerWrapper.Instance.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions), $"Installing {name} {version}...", cancellationToken, progress).ConfigureAwait(false); if (!deploymentResult.IsRegistered) { throw new InvalidOperationException("The package could not be registered."); } var findInstalled = PackageManagerWrapper.Instance.FindPackages(name, publisher).FirstOrDefault(); if (findInstalled == null) { throw new InvalidOperationException("The package could not be registered."); } var familyName = findInstalled.Id.FamilyName; await AsyncOperationHelper.ConvertToTask( PackageManagerWrapper.Instance.ProvisionPackageForAllUsersAsync(familyName), $"Provisioning {name} {version}...", cancellationToken, progress).ConfigureAwait(false); } else { var deploymentResult = await AsyncOperationHelper.ConvertToTask( PackageManagerWrapper.Instance.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions), "Installing " + name + "...", cancellationToken, progress).ConfigureAwait(false); if (!deploymentResult.IsRegistered) { var message = "Could not install " + name + " " + version + "."; if (!string.IsNullOrEmpty(deploymentResult.ErrorText)) { message += " " + deploymentResult.ErrorText; } if (deploymentResult.ExtendedErrorCode != null) { throw new InvalidOperationException(message, deploymentResult.ExtendedErrorCode); } throw new InvalidOperationException(message); } } } }
public Task Add(string filePath, AddAppxPackageOptions options = (AddAppxPackageOptions)0, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default)
public async Task Add(string filePath, AddAppxPackageOptions options = 0, CancellationToken cancellationToken = default, IProgress <ProgressData> progress = default) { if (SideloadingChecker.GetStatus() < SideloadingStatus.Sideloading) { throw new DeveloperModeException("Developer mode or sideloading must be enabled to install packages outside of Microsoft Store."); } Logger.Info("Installing package {0}", filePath); if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (string.Equals(Path.GetFileName(filePath), "AppxManifest.xml", StringComparison.OrdinalIgnoreCase)) { var reader = await AppxManifestSummaryBuilder.FromManifest(filePath).ConfigureAwait(false); DeploymentOptions deploymentOptions = 0; if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion; } if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= DeploymentOptions.ForceApplicationShutdown; deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown; } deploymentOptions |= DeploymentOptions.DevelopmentMode; await AsyncOperationHelper.ConvertToTask( PackageManager.Value.RegisterPackageAsync(new Uri(filePath), Enumerable.Empty <Uri>(), deploymentOptions), $"Installing {reader.DisplayName} {reader.Version}...", cancellationToken, progress).ConfigureAwait(false); } else if (string.Equals(".appinstaller", Path.GetExtension(filePath), StringComparison.OrdinalIgnoreCase)) { if (options.HasFlag(AddAppxPackageOptions.AllUsers)) { throw new NotSupportedException("Cannot install a package from .appinstaller for all users."); } AddPackageByAppInstallerOptions deploymentOptions = 0; if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { throw new NotSupportedException("Cannot force a downgrade with .appinstaller. The .appinstaller defines on its own whether the downgrade is allowed."); } if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= AddPackageByAppInstallerOptions.ForceTargetAppShutdown; } var volume = PackageManager.Value.GetDefaultPackageVolume(); await AsyncOperationHelper.ConvertToTask( PackageManager.Value.AddPackageByAppInstallerFileAsync(new Uri(filePath, UriKind.Absolute), deploymentOptions, volume), "Installing from " + Path.GetFileName(filePath) + "...", cancellationToken, progress).ConfigureAwait(false); } else { try { var reader = await AppxManifestSummaryBuilder.FromMsix(filePath).ConfigureAwait(false); DeploymentOptions deploymentOptions = 0; if (options.HasFlag(AddAppxPackageOptions.AllowDowngrade)) { deploymentOptions |= DeploymentOptions.ForceUpdateFromAnyVersion; } if (options.HasFlag(AddAppxPackageOptions.KillRunningApps)) { deploymentOptions |= DeploymentOptions.ForceApplicationShutdown; deploymentOptions |= DeploymentOptions.ForceTargetApplicationShutdown; } if (options.HasFlag(AddAppxPackageOptions.AllUsers)) { var deploymentResult = await AsyncOperationHelper.ConvertToTask( PackageManager.Value.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions), $"Installing {reader.DisplayName} {reader.Version}...", cancellationToken, progress).ConfigureAwait(false); if (!deploymentResult.IsRegistered) { throw new InvalidOperationException("The package could not be registered."); } var findInstalled = PackageManager.Value.FindPackages(reader.Name, reader.Publisher).FirstOrDefault(); if (findInstalled == null) { throw new InvalidOperationException("The package could not be registered."); } var familyName = findInstalled.Id.FamilyName; await AsyncOperationHelper.ConvertToTask( PackageManager.Value.ProvisionPackageForAllUsersAsync(familyName), $"Provisioning {reader.DisplayName} {reader.Version}...", cancellationToken, progress).ConfigureAwait(false); } else { var deploymentResult = await AsyncOperationHelper.ConvertToTask( PackageManager.Value.AddPackageAsync(new Uri(filePath, UriKind.Absolute), Enumerable.Empty <Uri>(), deploymentOptions), "Installing " + reader.DisplayName + "...", cancellationToken, progress).ConfigureAwait(false); if (!deploymentResult.IsRegistered) { throw new InvalidOperationException("The package could not be registered."); } } } catch (InvalidDataException e) { throw new InvalidOperationException("File " + filePath + " does not seem to be a valid MSIX package." + e, e); } } }
private async void OnAddPackage(string packagePath, bool forAllUsers) { if (packagePath == null) { if (forAllUsers) { if (!this.interactionService.SelectFile(FileDialogSettings.FromFilterString(new DialogFilterBuilder("*" + FileConstants.MsixExtension, "*" + FileConstants.AppxExtension).BuildFilter()), out packagePath)) { return; } } else { if (!this.interactionService.SelectFile( // ReSharper disable StringLiteralTypo FileDialogSettings.FromFilterString(new DialogFilterBuilder("*" + FileConstants.MsixExtension, "*" + FileConstants.AppxExtension, "*" + FileConstants.AppxBundleExtension, "*" + FileConstants.AppInstallerExtension, FileConstants.AppxManifestFile).BuildFilter()), out packagePath)) // ReSharper restore StringLiteralTypo { return; } } } AddAppxPackageOptions options = 0; if (forAllUsers) { options |= AddAppxPackageOptions.AllUsers; } options |= AddAppxPackageOptions.KillRunningApps; var context = this.busyManager.Begin(); try { using var wrappedProgress = new WrappedProgress(context); var p1 = wrappedProgress.GetChildProgress(90); var p2 = wrappedProgress.GetChildProgress(10); var manager = await this.packageInstallerProvider.GetProxyFor(forAllUsers?SelfElevationLevel.AsAdministrator : SelfElevationLevel.AsInvoker).ConfigureAwait(false); await manager.Add(packagePath, options, progress : p1).ConfigureAwait(false); AppxIdentity appxIdentity = null; if (!string.Equals(FileConstants.AppInstallerExtension, Path.GetExtension(packagePath), StringComparison.OrdinalIgnoreCase)) { appxIdentity = await new AppxIdentityReader().GetIdentity(packagePath).ConfigureAwait(false); #pragma warning disable 4014 this.interactionService.ShowToast("App installed", $"{appxIdentity.Name} has been just installed.", InteractionType.None); #pragma warning restore 4014 } else { #pragma warning disable 4014 this.interactionService.ShowToast("App installed", $"A new app has been just installed from {Path.GetFileName(packagePath)}.", InteractionType.None); #pragma warning restore 4014 } var allPackages = await this.application.CommandExecutor.Invoke <GetPackagesCommand, IList <InstalledPackage> >(this, new GetPackagesCommand(forAllUsers ? PackageFindMode.AllUsers : PackageFindMode.CurrentUser), progress : p2).ConfigureAwait(false); if (appxIdentity != null) { var selected = allPackages.FirstOrDefault(p => p.Name == appxIdentity.Name); if (selected != null) { //this.application.ApplicationState.Packages.SelectedPackages.Clear(); //this.application.ApplicationState.Packages.SelectedPackages.Add(selected); await this.application.CommandExecutor.Invoke(this, new SelectPackagesCommand(selected.ManifestLocation)).ConfigureAwait(false); } } else { await this.application.CommandExecutor.Invoke(this, new SelectPackagesCommand()).ConfigureAwait(false); } } catch (Exception exception) { Logger.Error(exception); this.interactionService.ShowError(exception.Message, exception); } finally { this.busyManager.End(context); } }