Development Tip – Packaging and loading files

In Windows 8 I want to have a file in my solution which is packaged as part of the application, then read at runtime. It’s not immediately obvious how to do this because the app is running in an environment we’re not used to, and using StorageFile.GetFileFromPathAsync doesn’t let you provide relative paths. So what do you do? Where is your app installed so that you can provide the full path?

Here’s the solution:

 // Assume the file in question is in a project folder called "Data",
 // named "MyData.txt"
 var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
 var file = await folder.GetFileAsync( @"Data\MyData.txt" );
 var winRTstream = await file.OpenAsync( FileAccessMode.Read );
 var oldStylestream = winRTstream.OpenRead();