Tips, tricks, and guides for developing on modern Windows platforms
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.
We need to do the following:
Create a new blank UWP project.
1. Select the
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.
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).
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();
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.
MainPage.xaml.cs so we can handle the different launch condition there.
Add the following
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.Path;
var file = (StorageFile)fileArgs.Files;
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.
To prove that we can do something with the clicked file, let’s load its contents into a TextBlock.
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
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.
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: