FileNotFoundException reading JSON file from Assets folder in Windows Store app
Asked Answered
M

1

11

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

Matabele answered 8/1, 2014 at 11:8 Comment(0)
K
26

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.

Kimmy answered 8/1, 2014 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.