在Windows Phone 8.1 Runtime中读取项目文件夹中的文本文件

Bol*_*lta 14 c# windows-phone windows-runtime windows-phone-8.1 win-universal-app

我希望在第一次启动应用程序时将我的项目的根文件夹中的一个文件.txt读入我的数据库,但我不知道该怎么做.任何人都知道我该怎么做,请帮助我...谢谢

我在Windows Phone 8.1 Runtime中工作.

Rom*_*asz 33

如果您想从项目中读取文件,您可以这样做:

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();
Run Code Online (Sandbox Code Playgroud)

另外,请确保您已将文件的构建操作设置为内容(它应该是默认值).

有关URI方案的更多信息,请访问MSDN.


Jed*_*dja 9

访问文件的方式略有不同:

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