FileNotFoundException reading JSON file from Assets folder in Windows Store app

别等时光非礼了梦想. 提交于 2019-11-26 21:47:57

问题


I'm trying to read a json file from my Assets folder. I've tried numerous code examples, and all are variations on the same thing.

I feel like I must be doing something stupid, but I just can't see it.

string filepath = @"Assets\resources.json";
StorageFolder folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await folder.GetFileAsync(filepath); // error here
var data = await Windows.Storage.FileIO.ReadTextAsync(file);

Every time I run the above code I get a System.IO.FileNotFoundException on my app. I've double and tripled checked the file is in the assets folder and there are no typos in my path.

I've also tried this

StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/resources.json"));

Same error. System.IO.FileNotFoundException

Screenshot of my Assets folder:


回答1:


In the properties of the file (Right click and properties) under Build Action have you made sure this is selected as Content? Also make sure that Copy to Output Directory is set as Copy Always. This should solve the problem for you I think.



来源:https://stackoverflow.com/questions/20993849/filenotfoundexception-reading-json-file-from-assets-folder-in-windows-store-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!