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:

Setup the filetype association in your app’s package manifest. 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 https://msdn.microsoft.com/en-us/library/windows/apps/mt269385.aspx 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)

{

base.OnFileActivated(args);

var rootFrame = new Frame();

rootFrame.Navigate(typeof(MainPage), args);

Window.Current.Content = rootFrame;

Window.Current.Activate();

}



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)

{

base.OnNavigatedTo(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 http://www.rikeripsum.com/#!/), 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: