Cannot see JSON file in project

using VS 2013, C #

I am trying to use a JSON file in my project. Using the following code (as in the example 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(); 

The result is:

enter image description here

and the next step is the exception file was not found, but it exists

enter image description hereenter image description here

In addition, if the same code is run with a sample project from VS - all this is normal. Code from the 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 the resulting code:

enter image description here

So the question is: why did I get a FileNotFound exception, why this file is invisible to my project.

+8
json c # visual-studio-2013
source share
1 answer

Oh obvious reason

If you want to use a JSON file, you need to do the following: In the properties settings of Build Action: Content and Copy to Output Directory: Copy Always

enter image description here

Also found the answer here . This is also found and this link is useful. Maybe this can be useful for someone.

+11
source share

All Articles