Read text file in project folder in Windows Phone 8.1 Runtime

Bolt Delta picture Bolt Delta · May 29, 2014 · Viewed 12.9k times · Source

I want read one file .txt in root folder of my project into my database at first time application launch, but I don't know how to do that. Anyone know how can I do that, please help me... Thanks

I'm working in Windows Phone 8.1 Runtime.

Answer

Romasz picture Romasz · May 30, 2014

If you want to read a file from your project you can for example do it like this:

string fileContent;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///example.txt"));
using (StreamReader sRead = new StreamReader(await file.OpenStreamForReadAsync()))
fileContent = await sRead.ReadToEndAsync();

Also please ensure that you have set the Build Action of your file as Content (it should be as default).

More about URI schemes you will find here at MSDN.