1

(I can find many answers for Windows Phone 7 which probably work Windows Phone 8/8.1 Silverlight, but not for Windows Phone [Store] 8.1)

While testing my application, I want to use a dummy server response. Since it's a large amount of data that includes quotation marks, I don't want to use a constant string and have to escape everything.

How can I read a text file that's included with my Windows Phone 8.1 application?

Jedidja
  • 16,610
  • 17
  • 73
  • 112
  • possible duplicate of [Read text file in project folder in Windows Phone 8.1 Runtime](http://stackoverflow.com/questions/23924424/read-text-file-in-project-folder-in-windows-phone-8-1-runtime) – Jedidja Jun 06 '14 at 12:51

1 Answers1

1

For this example, I have a file called sample-response.txt and its 'Build Action' properties is set to 'Content'.

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

and if you want to double-check that it's been read ok

Debug.WriteLine(contents);
Jedidja
  • 16,610
  • 17
  • 73
  • 112
  • You can also use Uri to get the file - [example](http://stackoverflow.com/a/23951961/2681948) if you need it. – Romasz Jun 06 '14 at 12:37
  • Wow; all that searching and I was never able to find that question. I'll vote to close mine as duplicate. – Jedidja Jun 06 '14 at 12:50