How can I check for updates from within a Windows app in .NET MAUI?

Lohith K K 105 Reputation points
2024-09-28T19:13:18.2666667+00:00

I am developing a windows application using .NET MAUI planform. I am deploying it to the user using the appinstaller file. I configured the appinstaller file to check for update in the background every 8 hour once. What I also need is to check for update from within the application also. Like clicking a button and checking for the update and show the dialogue for users to update the same.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
784 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,471 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rolle Michel 75 Reputation points
    2024-09-29T05:22:21.6866667+00:00

    To manually check for updates from within a .NET MAUI Windows application, you can leverage the AppInstaller file and a bit of code to trigger the update check on demand. Since .NET MAUI doesn’t natively support app updates, you can work with Windows features for this, specifically using the AppInstaller API.

    Here’s a general approach to achieve this:

    1. Use AppUpdateOptions and AppInstaller API:

    You can manually trigger a check for updates programmatically using the Windows AppInstaller API in combination with the AppUpdateOptions.

    Steps:

    1.	Add the required references:
    

    Make sure you add a reference to the Microsoft.Windows.AppInstaller API, which is part of the Microsoft.Windows.SDK.Contracts or Windows.ApplicationModel.Store.Preview.InstallControl namespace.

    2.	Invoke the update check manually:
    

    You can trigger the update check by calling the CheckForUpdateAsync() method from the AppInstallerManager class.

    Sample Code:

    using Windows.ApplicationModel.Store.Preview.InstallControl;

    using System.Threading.Tasks;

    public class UpdateService

    {

    public async Task CheckForUpdatesAsync()
    
    {
    
        var manager = AppInstallerManager.GetDefault();
    
        // Check for updates asynchronously
    
        var updateInfo = await manager.CheckForUpdatesAsync();
    
        if (updateInfo.Status == AppInstallerStatus.Available)
    
        {
    
            // If an update is available, initiate the update process
    
            var result = await manager.StartProductInstallAsync(
    
                updateInfo.ProductId,
    
                updateInfo.Version,
    
                null,
    
                true,  // Auto-restart the app if needed
    
                true   // Show UI if applicable
    
            );
    
            if (result == AppInstallerStatus.NoError)
    
            {
    
                // Notify user about the update and optionally restart the app
    
            }
    
        }
    
        else
    
        {
    
            // Notify user that the app is up to date
    
        }
    
    }
    

    }

    1. Hook into a Button Click Event in Your MAUI App:

    Once you have the above CheckForUpdatesAsync() function, you can hook it up to a button in your MAUI application:

    private async void OnCheckForUpdatesClicked(object sender, EventArgs e)

    {

    var updateService = new UpdateService();
    
    await updateService.CheckForUpdatesAsync();
    

    }

    And in your XAML for the button:

    <Button Text="Check for Updates" Clicked="OnCheckForUpdatesClicked" />

    Key Points:

    •	This code will allow you to check for updates on demand.
    
    •	The AppInstallerManager handles the interaction with the AppInstaller file, so the update URL and other details are pulled from it.
    
    •	The user will be shown the default Windows UI for updates when an update is available.
    

    Make sure your AppInstaller file is properly configured to allow updates, and the endpoint URL for updates is accessible.

    If my answer is helpful to you, you can adopt it, thank you!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.