update uwp app from usb drive on raspberry pi

本小妞迷上赌 提交于 2019-11-29 17:00:48

I want to add an update button to my app and when pressed it would look for a USB stick and check for a file.

The packagemanager.UpdatePackageAsync API can help you do this in your UWP app and update itself.

But you can't simply "look for a USB stick and check for a file" like you can do on the desktop via FilePicker that not supported on Windows IoT Core. Here I show a sample to specify the file location and version then update it.

To use this API you need to add the packageManagement capability in Package.appxmanifest like the followings:

...   
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 

IgnorableNamespaces="uap mp rescap">

...

  <Capabilities>
    <rescap:Capability Name="packageManagement" />
  </Capabilities>

There is a code sample you can reference:

MainPage.xaml

<StackPanel VerticalAlignment="Center">
    <Button Content="Update" Click="Button_Click"/>
    <TextBox Name="NewVersion" PlaceholderText="For example: 1.0.5.0"/>
    <TextBox Name="PkgPath" PlaceholderText="For example: D:\AppUpdate"/>
    <TextBlock Text="Install result: " Name="Result" />
</StackPanel>

MainPage.xaml.cs

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            string versionNum = NewVersion.Text;
            string packagePath = PkgPath.Text; 
            string packageLocation = packagePath + @"\TestAppUpdate_" + versionNum + "_x86_x64_arm_Debug.appxbundle";
            PackageManager packagemanager = new PackageManager();
            await packagemanager.UpdatePackageAsync(new Uri(packageLocation), null, DeploymentOptions.ForceApplicationShutdown);
        }
        catch (Exception ex)
        {
            Result.Text = ex.Message;
        }
    }

The app will update and auto restart to the new version.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!