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

时间:2014-05-29 01:35:51

标签: c# windows-runtime windows-phone windows-phone-8.1 win-universal-app

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

我在Windows Phone 8.1 Runtime中工作。

3 个答案:

答案 0 :(得分:34)

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

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

另外,请确保您已将文件的 Build Action 设置为 Content (它应该是默认设置)。

有关URI schemes you will find here at MSDN的更多信息。

答案 1 :(得分:9)

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

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

答案 2 :(得分:1)

它只能在Windows Phone 8.1中运行。以前版本的Windows手机(Windows Phone 8Windows Phone 7)都无法运行您的应用。