static async Task <bool> AddHostedPackageAsync(string pathToPackage, bool isPackageFile) { var packageUri = new Uri(pathToPackage); Console.WriteLine(" Package Address {0}", pathToPackage); Console.WriteLine(" Package Uri {0}", packageUri); PackageManager packageManager = new PackageManager(); Task <DeploymentResult> deployment = null; // Set AllowUnsigned=true for unsigned Hosted Apps if (isPackageFile) { var addOptions = new AddPackageOptions { AllowUnsigned = true }; deployment = packageManager.AddPackageByUriAsync(packageUri, addOptions).AsTask(); } else { var regOptions = new RegisterPackageOptions { AllowUnsigned = true }; deployment = packageManager.RegisterPackageByUriAsync(packageUri, regOptions).AsTask(); } Console.WriteLine("Installing package {0}", packageUri); Debug.WriteLine("Waiting for package registration to complete..."); try { await deployment; Console.WriteLine("Registration succeeded! Try running the new app."); return(true); } catch (OperationCanceledException) { Console.WriteLine("Registration was canceled"); } catch (AggregateException ex) { Console.WriteLine($"Installation Error: {ex}"); } return(false); }
private static bool registerSparsePackage(string externalLocation, string sparsePkgPath) { bool registration = false; try { Uri externalUri = new Uri(externalLocation); Uri packageUri = new Uri(sparsePkgPath); Console.WriteLine("exe Location {0}", externalLocation); Console.WriteLine("msix Address {0}", sparsePkgPath); Console.WriteLine(" exe Uri {0}", externalUri); Console.WriteLine(" msix Uri {0}", packageUri); PackageManager packageManager = new PackageManager(); //Declare use of an external location var options = new AddPackageOptions(); options.ExternalLocationUri = externalUri; Windows.Foundation.IAsyncOperationWithProgress <DeploymentResult, DeploymentProgress> deploymentOperation = packageManager.AddPackageByUriAsync(packageUri, options); ManualResetEvent opCompletedEvent = new ManualResetEvent(false); // this event will be signaled when the deployment operation has completed. deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); }; Console.WriteLine("Installing package {0}", sparsePkgPath); Debug.WriteLine("Waiting for package registration to complete..."); opCompletedEvent.WaitOne(); if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Error) { Windows.Management.Deployment.DeploymentResult deploymentResult = deploymentOperation.GetResults(); Debug.WriteLine("Installation Error: {0}", deploymentOperation.ErrorCode); Debug.WriteLine("Detailed Error Text: {0}", deploymentResult.ErrorText); } else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Canceled) { Debug.WriteLine("Package Registration Canceled"); } else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Completed) { registration = true; Debug.WriteLine("Package Registration succeeded!"); } else { Debug.WriteLine("Installation status unknown"); } } catch (Exception ex) { Console.WriteLine("AddPackageSample failed, error message: {0}", ex.Message); Console.WriteLine("Full Stacktrace: {0}", ex.ToString()); return(registration); } return(registration); }