Skip to content

Latest commit

 

History

History
102 lines (83 loc) · 4.82 KB

package_checkupdateavailabilityasync_726867427.md

File metadata and controls

102 lines (83 loc) · 4.82 KB
-api-id -api-type ms.custom
M:Windows.ApplicationModel.Package.CheckUpdateAvailabilityAsync
winrt method
RS5

Windows.ApplicationModel.Package.CheckUpdateAvailabilityAsync

-description

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.

-returns

A PackageUpdateAvailabilityResult that indicates if an application has an update, and if the update is required.

-remarks

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.

-see-also

PackageUpdateAvailabilityResult,PackageManager.FindPackageForUser,App Installer APIs

-examples

Note

The MSIX Labs for Developers repo contains additional examples on modernizing desktop apps by taking advantage of MSIX, including an exercise using the embedded .AppInstaller feature.

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.
    var 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.
    var 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;
    }
}