14

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
  • 215
  • 1
  • 3
  • 10
  • 1
    What do you mean by old version (WP8.1 Runtime is quite new)? Do you get any exceptions? Have you added file assotiations? – Romasz May 29 '14 at 04:30

3 Answers3

32

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.

Jedidja
  • 16,610
  • 17
  • 73
  • 112
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • @Jacek There are couple of ways to do it, you should easily find help in internet: [one](http://stackoverflow.com/q/19596135/2681948), [two](http://stackoverflow.com/q/13297563/2681948), [three](http://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm). – Romasz Mar 05 '15 at 13:47
  • @Romasz +1 for "Build Action" – Amit Jun 24 '15 at 17:47
9

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);
Jedidja
  • 16,610
  • 17
  • 73
  • 112
1

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.

Kalyan
  • 1,395
  • 2
  • 13
  • 26