简体   繁体   中英

How do I read an embedded text file on Windows Phone 8.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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM