Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileNotFoundException reading JSON file from Assets folder in Windows Store app

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:

Screenshot of my Assets folder

like image 383
roryok Avatar asked Jan 08 '14 11:01

roryok


1 Answers

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.

like image 95
TylerD87 Avatar answered Nov 16 '22 08:11

TylerD87