我希望在首次启动应用程序时将项目的根文件夹中的一个文件.txt读入我的数据库,但我不知道该怎么做。任何人都知道我该怎么做,请帮助我...谢谢
我在Windows Phone 8.1 Runtime中工作。
答案 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 (它应该是默认设置)。
答案 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 8
,Windows Phone 7
)都无法运行您的应用。