Opening Files in the Default Viewer from a Windows 8 Metro Application

April 7th 2012 Windows Store

Since Metro applications are running in a sandboxed environment they only have limited file handling capabilities. The private storage area for each application is conceptually very similar to isolated storage as we already know it from Silverlight and Windows Phone 7, only with its own API. The actual storage location is slightly different as well (although it is abstracted away, it's useful to be able to look into it during development):

  • Metro application files are located in C:\Users\<ProfileName>\AppData\Local\Packages
  • .NET isolated storage files are located in C:\Users\<ProfileName>\AppData\Local\Isolated Storage
  • Silverlight isolated storage files are located in C:\Users\<ProfileName>\AppData\Local\Microsoft\Silverlight

This still doesn't solve the problem of opening a file in a standard format (PDF for instance) with the associated application. In .NET you could simply use the Process type:

Process.Start(filename);

Unfortunately it isn't available for Metro style applications, therefore a different solution is required. My first instinct was to activate a Windows 8 contract but it turned out none of them can be used for this purpose. It turns out the solution is even simpler - Launcher class:

StorageFile file = await ApplicationData.LocalFolder.GetFileAsync(filename);
await Launcher.LaunchFileAsync(file);

There are also a couple of overloads available for setting additional options and opening files from Uris.

Get notified when a new blog post is published (usually every Friday):

Copyright
Creative Commons License