Developing for Modern Windows

Tips, tricks, and guides for developing on modern Windows platforms

Open your App with a File Association (Windows 10 UWP)

When you double-click an .mp3 file you expect your music player to play the song; when you double-click an .xls file you expect Excel to open the spreadsheet. Wouldn’t it be cool to do this in your own Universal Windows Platform (UWP) apps?

It’s actually quite easy to do. Follow this tutorial to learn how to launch your app by double-clicking an associated file (and your app can do something with that file of course). This tutorial is suitable for beginners with a little bit of Windows app development experience.

The Steps

We need to do the following:

  1. Setup the filetype association in your app’s package manifest.
  2. Detect when the app was launched via a file and handle that appropriately.

Create a new Project

Create a new blank UWP project.

Setup the Filetype Association

Open Package.appxmanifest:


1. Select the Declarations tab

2. Choose File Type Associations from the Available Declarations: drop-down list.

3. Click the Add button.


The error (cross in red circle) icons indicate that you have some information to fill in to complete the association.

You can add multiple associations, for example an image editor app can associate .png, .jpg, .gif, etc.

Fill in the relevant details:

(see for detailed explanations of all the fields).

1. Type your app’s display name (this shows when the user is asked to choose which app to open a file with).

2. Add a thumbnail image for files associated with your app. You can click the browse button to find an image.

3. Add an info tip to show when the user hovers the mouse cursor over a file.

4. Give the file type a name. This must be in lowercase.

5. Select Open is safe.

6. Type text/plain in the Content type field.

7. Finally, enter the file extension you want to use to launch your app in the File type field (I used .fas – file access sample).

Save Package.appxmanifest (Ctrl-S).



Handle the Launch

Now that the app is associated with .fas files, we need to tell it what to do when it is launched by clicking one of those files.

Open up App.xaml.cs, and add the following method:

protected override void OnFileActivated(FileActivatedEventArgs args)
    var rootFrame = new Frame();
    rootFrame.Navigate(typeof(MainPage), args);
    Window.Current.Content = rootFrame;

This method automatically executes when the application is launched as the result of double-clicking an associated filetype. FileActivatedEventArgs contains information about the file that was clicked.  The method opens an instance of MainPage, and sends the FileActivatedEventArgs to it.

You could of course put any code in this method to handle the launch. For example you could navigate to a page other than MainPage.

Modify MainPage

Now open MainPage.xaml.cs so we can handle the different launch condition there.

Add the following OnNavigatedTo method:

protected override async void OnNavigatedTo(NavigationEventArgs e)
    var args = e.Parameter as Windows.ApplicationModel.Activation.IActivatedEventArgs;
    if (args != null)
        if (args.Kind == Windows.ApplicationModel.Activation.ActivationKind.File)
            var fileArgs = args as Windows.ApplicationModel.Activation.FileActivatedEventArgs;
            string strFilePath = fileArgs.Files[0].Path;
            var file = (StorageFile)fileArgs.Files[0];
            await LoadFasFile(file);
You also need to add the Windows.Ui.Xaml.Navigation and System.Threading.Tasks namespaces.
The await LoadFasFile(file) line will show an error – because we haven’t implemented this method yet.

This code finds the file that was clicked to launch the app (if the app was launched that way), then sends it to the LoadFasFile method, which we’ll create next.

Load the File

To prove that we can do something with the clicked file, let’s load its contents into a TextBlock.

Add a TextBlock to MainPage.xaml called “FileContents”. You can paste this line of XAML straight into the main Grid:

<TextBlock x:Name="FileContents" FontSize="21.333" TextWrapping="Wrap"/>

Now add this method to MainPage.xaml.cs:

private async Task LoadFasFile(StorageFile file)
    var read = await FileIO.ReadTextAsync(file);
    FileContents.Text = read;

That little method just loads the contents of file into the FileContents TextBlock you added to MainPage earlier.

Try it Out

The project is done! Create an ordinary text file and put any text you want in there (I always generate random text at!/), and save the file with the required extension (.fas if you’re following along).

Deploy the app to your local machine, then double-click on the .fas file you just created. Your app will launch, and the contents of the clicked file will be displayed in the window:


Tags: , ,

6 Responses to “Open your App with a File Association (Windows 10 UWP)”

  1. Manish says:

    Thanks for this amazing tutorial.
    But i want to open .mp4 file in media element.
    please help me.

  2. Raimund Popp says:

    this is a top explained tutorial in all details. specially all little steps to launch the file-app-onfileactivated-navigate to mainpage

  3. Nilay Mehta says:

    Thanks a lot it’s really helpful for me for video 😉
    For that i was copy file to LocalFolder and set source to MediaElement
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    await file.CopyAsync(localFolder, file.Name, NameCollisionOption.ReplaceExisting);
    localSettings.Values[“video_uri”] = file.Name;

  4. Geo says:

    This is a lifesaver.. Thank you for this very clear and to-the-point tutorial.

  5. Philip Littlewood says:

    This works OK if the app is not open. However, if you click on a file when the app is already open it fails.

    • Damien says:

      I’ve just tested it, and it works fine for me. I did the following:

      1) Double-clicked a test file with extension .FAS (the extension I set up for the app to use).
      2) App opened and displayed content from the file.
      3) Left the app open and displaying the content of the file.
      4) Double-clicked another file with the .FAS extension
      5) App opened the second file and displayed its contents.

      How does it fail for you? What exactly are you doing? Have you changed the code from the blog post at all? Are you on a retail version of Windows 10 (i.e. not an insider preview)?

Leave a Reply

Your email address will not be published.