Read text file in project folder in Windows Phone 8.1 Runtime

12,919

Solution 1

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.

Solution 2

A slightly different way to access the file:

var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync("sample.txt");
var contents = await Windows.Storage.FileIO.ReadTextAsync(file);

Solution 3

It can only run in Windows Phone 8.1. No previous version of windows phone (Windows Phone 8, Windows Phone 7) would be able to run your app.

Share:
12,919
Bolt Delta
Author by

Bolt Delta

I'm number one.

Updated on July 03, 2022

Comments

  • Bolt Delta
    Bolt Delta almost 2 years

    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.

  • Bolt Delta
    Bolt Delta almost 10 years
    Sorry you, I think my question so confusing, and I fixed it already, thank you for help!
  • Romasz
    Romasz about 9 years
    @Jacek There are couple of ways to do it, you should easily find help in internet: one, two, three.
  • Saeed Raffoul
    Saeed Raffoul almost 9 years
    @Romasz +1 for "Build Action"