How to launch a file with different apps in UWP using C#

Let’s say, there is a case when we need to launch a file and not just to launch it with default application but using other apps which can handle this file’s format. This might happen when we’d like to open a PDF file using some reader apps or when we simply would like to edit PNG file in photo editors instead of launching it with a photo viewer.

First, we need to know what apps can launch our file. It can be achieved by getting a list of file’s handlers:

This list contains only information about apps from the Store because of UWP is sandboxed, so if you run this part of code on PC don’t expect to see win32 apps in this list.

Each element of this list has two parameters which are valuable to us:

  • PackageFamilyName is an identifier of the app’s package
  • DisplayInfo, has user friendly description of the app (including title)

While the second parameter serves as some sort of decoration for UI, the first one does all hidden to user’s eyes work.

Next thing is setting launcher options which specify the app which is going to launch our file and other useful parameters:

In this example, I’m using only several options which, in my opinion, are the most necessary. Shortly describing what each does:

  • TreatAsUntrusted gets or sets a value that indicates whether the system should display a warning that the file is potentially unsafe
  • DesiredRemainingView decides the currently source app’s window size on the screen
  • TargetApplicationPackageFamilyName gets or sets the target app that should be used to launch our file

If there’s no UWP application which can handle our file, then TargetApplicationPackageFamilyName should be empty. In this case the app will be launched with default application.

And after all this done we can simply launch our file:

That’s it, now your file should be opened with chosen app.

As I mentioned before, there is no way to put win32 app’s package info into TargetApplicationPackageFamilyName, but there’s a workaround.

There is an option in LauncherOptions which can show “Open With” dialog where you can pick win32 or Store app:

It might be not the best way to do it, but that’s the only way (at least which I know). If you’re going to use it, don’t forget to delete a string with TargetApplicationPackageFamilyName, otherwise “Open With” dialog won’t be shown.

Here’s a GitHub project, where you also find the way how to show associated apps to user, so he could pick one.

You may also like...