To do that you need
- to include the file in the project
- to read the data
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.