C# 在Windows Phone 8.1运行时读取项目文件夹中的文本文件

C# 在Windows Phone 8.1运行时读取项目文件夹中的文本文件,c#,windows-runtime,windows-phone,windows-phone-8.1,win-universal-app,C#,Windows Runtime,Windows Phone,Windows Phone 8.1,Win Universal App,我想在第一次启动应用程序时,将项目根文件夹中的一个文件.txt读入数据库,但我不知道如何做到这一点。谁知道我该怎么做,请帮帮我。。。谢谢 我正在Windows Phone 8.1运行时中工作。它只能在Windows Phone 8.1中运行。以前版本的windows phone(windows phone 8,windows phone 7)都无法运行你的应用程序。如果你想从项目中读取文件,你可以这样做: string fileContent; StorageFile file = await

我想在第一次启动应用程序时,将项目根文件夹中的一个文件.txt读入数据库,但我不知道如何做到这一点。谁知道我该怎么做,请帮帮我。。。谢谢


我正在Windows Phone 8.1运行时中工作。

它只能在
Windows Phone 8.1
中运行。以前版本的windows phone(
windows phone 8
windows phone 7
)都无法运行你的应用程序。

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

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();
另外,请确保已将文件的生成操作设置为内容(应为默认设置)


更多信息。

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

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

您所说的旧版本(WP8.1运行时是全新的)是什么意思?你有例外吗?你添加了文件关联吗?我编辑了我的问题…对不起,我觉得我的问题太混乱了,我已经解决了,谢谢你的帮助@Jacek有两种方法可以做到这一点,你应该很容易在互联网上找到帮助:,@Romasz+1代表“构建操作”