Sunday, September 8, 2013

Reading Text from an embedded file in a WinRT application

When you want to provide data to a WinRT application (a.k.a. Windows Store App) it is sometimes useful to embed a file in your application package. This file may for instance contain help information, initial user settings, sample data...

To do that you need
  1. to include the file in the project
  2. to read the data
Let's make an example using a text file.
To include the text file in the application, just add the text file to the solution

Note that I have created a folder to put my text files .This avoid cluttering the project with too many files.

It is important to correctly select the build action Content to ensure that the file shall effectively be embedded in the application file.

Now that the file is included in the application package we must get access to it.
The code below shows how to read the whole text file and display its first two lines in textblocks
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            //retrieve the text file content
            Uri uri = new Uri("ms-appx:///MyTextFiles/MyTextFile1.txt");
            StorageFile file =
                await StorageFile.GetFileFromApplicationUriAsync(uri);
            IList<String> lines = await FileIO.ReadLinesAsync(file);

            //use the retrieved data
            TextBlock1.Text = lines[0];
            TextBlock2.Text = lines[1];
        }

Note the usage of an URI instead of a path to identify the file. Embedded files cannot be accessed using a file path.

Note also the very strange URI syntax with a triple slash ms-appx:///

Obviously, the same technique can be used to embed other things than text files. You could embed images, sounds or binary data.
I hope this will help you the next time you need to embed a data file inside a Windows store application.

No comments:

Post a Comment