在Windows Phone 8.1 Runtime中读取项目文件夹中的文本文件
我希望在第一次启动应用程序时将我项目的根文件夹中的一个文件.txt读入我的数据库,但我不知道该怎么做。 任何人都知道我该怎么做,请帮助我……谢谢
我在Windows Phone 8.1 Runtime中工作。
如果要从项目中读取文件,可以这样做:
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();
另外,请确保您已将文件的构建操作设置为内容 (它应该是默认值)。
有关URI方案的更多信息, 请访问MSDN 。
访问文件的方式略有不同:
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation; var file = await folder.GetFileAsync("sample.txt"); var contents = await Windows.Storage.FileIO.ReadTextAsync(file);
它只能在Windows Phone 8.1
运行。 没有以前版本的Windows手机( Windows Phone 8
, Windows Phone 7
)可以运行您的应用程序。