How to remove a package
[This documentation is preliminary and is subject to change.]
Learn how to remove an app package using the package deployment API.
Instructions
Step 1: Get the full name of the package
Prompt your user for the full name of the package to be removed. For example, sampleapp_1.0.0.0_neutral_en-us_ab1c2d3efghij. Here's an example from a command-line app that stores the URI in the variable named inputPackageFullName
.
if (args.Length < 1)
{
Console.WriteLine("Usage: RemovePackageSample.exe packageFullName");
return 1;
}
string inputPackageFullName = args[0];
if ( args->Length < 2 )
{
wcout << "Usage: RemovePackageSample.exe packageFullName" << endl;
return 1;
}
String^ inputPackageFullName = args[1];
Step 2: Create a package manager object
Declare a variable of type Windows.Management.Deployment.PackageManager.
using Windows.Management.Deployment;
PackageManager packageManager = new PackageManager();
using namespace Windows::Management::Deployment;
PackageManager^ packageManager = ref new PackageManager();
Step 3: Start the deployment operation
Call the PackageManager.RemovePackageAsync method to uninstall the app package. The parameter specifies the package full name in inputPackageFullName
, which we created in the first step.
RemovePackageAsync returns an object that you can use to manage the asynchronous operation. Use the Completed property to set the event handler.
IAsyncOperationWithProgress<DeploymentResult, DeploymentProgress> deploymentOperation =
packageManager.RemovePackageAsync(inputPackageFullName);
ManualResetEvent opCompletedEvent = new ManualResetEvent(false); // signaled when the operation completes
deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); };
opCompletedEvent.WaitOne();
auto deploymentOperation = packageManager->RemovePackageAsync(inputPackageFullName);
DeploymentResult^ deploymentOperationResult;
opCompletedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
deploymentOperation->Completed =
ref new AsyncOperationWithProgressCompletedHandler<DeploymentResult^, DeploymentProgress>(
[&](IAsyncOperationWithProgress<DeploymentResult^, DeploymentProgress>^ operation, AsyncStatus)
{
SetEvent(opCompletedEvent);
});
WaitForSingleObject(opCompletedEvent, INFINITE);
Step 4: Check the results of the asynchronous deployment operation
Check DeploymentOperation.Status to determine the status of the deployment operation. The example calls the DeploymentOperation.GetResults method to get additional error information.
if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Error)
{
DeploymentResult deploymentResult = deploymentOperation.GetResults();
Console.WriteLine("Error code: {0}", deploymentOperation.ErrorCode);
Console.WriteLine("Error text: {0}", deploymentResult.ErrorText);
}
else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Canceled)
{
Console.WriteLine("Removal canceled");
}
else if (deploymentOperation.Status == Windows.Foundation.AsyncStatus.Completed)
{
Console.WriteLine("Removal succeeded");
}
else
{
Console.WriteLine("Removal status unknown");
}
if ( deploymentOperation->Status == Windows::Foundation::AsyncStatus::Error )
{
auto deploymentResult = deploymentOperation->GetResults();
wcout << L"Error code: " << deploymentResult->ErrorText->Data() << endl;
wcout << L"Error text: " << deploymentResult->ErrorText->Data() << endl;
}
else if ( deploymentOperation->Status == Windows::Foundation::AsyncStatus::Canceled )
{
wcout << L"Removal Canceled" << endl;
}
else if ( deploymentOperation->Status == Windows::Foundation::AsyncStatus::Completed )
{
wcout << L"Removal succeeded" << endl;
}
else
{
wcout << L"Removal status unknown" << endl;
}
Remarks
You can't use the deployment API in a Metro style app.
Complete example
To download the complete sample, see Remove app package sample.
Related topics
Concepts
Reference
Windows.Management.Deployment.PackageManager
Send comments about this topic to Microsoft
Build date: 4/20/2012