hbk
hbk

Reputation: 11184

Cant see JSON file in project

using VS 2013, C#

I am trying to use JSON file in my project. Using next code (as in sample project from MS):

        Uri dataUri = new Uri("ms-appx:///DataModel/MyData.json");
        StorageFile fileToRequest = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
        string jsonText = await FileIO.ReadTextAsync(fileToRequest);
        JsonObject jsonO = JsonObject.Parse(jsonText);
        JsonArray jsonA = jsonO["Events"].GetArray();

As result got this:

enter image description here

and next step - exception file not found, but it exists

enter image description here enter image description here

Also, if the same code launched with sample project from VS - all its ok. Code from sample project:

        Uri dataUri = new Uri("ms-appx:///DataModel/SampleData.json");
        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri);
        string jsonText = await FileIO.ReadTextAsync(file);
        JsonObject jsonObject = JsonObject.Parse(jsonText);
        JsonArray jsonArray = jsonObject["Groups"].GetArray();

Debugging of this code got:

enter image description here

So question - why I got exception FileNotFound, why this file invisible for my project.

Upvotes: 8

Views: 2831

Answers (1)

hbk
hbk

Reputation: 11184

Oh finnally found reason

If you want to use JSON file need to make next: In properties set Build Action: Content, and Copy to Output Directory: Copy Always

enter image description here

Also found answer here. Also found this and this link useful. Maybe it can be helpful for someone.

Upvotes: 11

Related Questions