-api-id | -api-type | ms.custom |
---|---|---|
M:Windows.ApplicationModel.Package.CheckUpdateAvailabilityAsync |
winrt method |
RS5 |
The CheckUpdateAvailabilityAsync method allows developers to check for updates to the main app package listed in the .appinstaller file. It allows the developer to determine if the updates are required due to .appinstaller policy. This method currently only works for applications installed via .appinstaller files.
A PackageUpdateAvailabilityResult that indicates if an application has an update, and if the update is required.
If you try to use this method on the Package object returned by the Current property, this method will fail with an "Access denied" error. This is a known issue that may be fixed in a future release. The example on this page demonstrates how to retrieve update information about the current app's package.
This method is not supported in JavaScript. However, you can create a Windows Runtime component that calls this method and then call this component from a JavaScript UWP app. For more information, see App Installer file API issues.
PackageUpdateAvailabilityResult,PackageManager.FindPackageForUser,App Installer APIs
An app developer wants to have a button in their app that allows a user to check for app updates. To enable the app to check if an update is available, they use the CheckUpdateAvailabilityAsync method as shown below.
private async void CheckForUpdatesButton_Click(object sender, RoutedEventArgs e)
{
// Get the current app's package for the current user.
PackageManager pm = new PackageManager();
Package currentPackage = pm.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
PackageUpdateAvailabilityResult result = await currentPackage.CheckUpdateAvailabilityAsync();
switch (result.Availability)
{
case PackageUpdateAvailability.Available:
GoToUpdateAvailableUIView();
break;
case PackageUpdateAvailability.Required:
GoToUpdateRequiredUIView();
break;
case PackageUpdateAvailability.NoUpdates:
// Dismissable ‘Ok’ dialog.
ShowNoUpdateAvailableDialog();
break;
case PackageUpdateAvailability.Unknown:
default:
// Log and ignore error.
Logger.Log($"No update information associated with app {Package.Current.DisplayName}");
// Dismissable ‘Ok’ dialog.
ShowNoUpdateAvailableDialog();
break;
}
}
From inside the app, the developer wants to check for updates and start the update process if updates are available.
public async void CheckForAvailableUpdatesAndLaunchAsync(string targetPackageFullName)
{
// Get the current app's package for the current user.
PackageManager pm = new PackageManager();
Package package = pm.FindPackageForUser(string.Empty, targetPackageFullName);
PackageUpdateAvailabilityResult result = await package.CheckUpdateAvailabilityAsync();
switch (result.Availability)
{
case PackageUpdateAvailability.Available:
GoToUpdateAvailableUIView();
break;
case PackageUpdateAvailability.Required:
GoToUpdateRequiredUIView();
break;
case PackageUpdateAvailability.NoUpdates:
// Launch target app and close AppInstaller.
LaunchTargetApp(targetPackageFullName);
await ConsolidateAppInstallerView();
break;
case PackageUpdateAvailability.Unknown:
default:
// Log and ignore error.
Logger.Log($"No update information associated with app {targetPackageFullName}");
// Launch target app and close AppInstaller.
LaunchTargetApp(targetPackageFullName);
await ConsolidateAppInstallerView();
break;
}
}