Search code examples
c#visual-studiowin-universal-app

C# - Reading from JSON file


I have problem with reading JSON file. File is inserted into Solution Explorer with Build Action set to Content and copy property se to Copy Always. I would expect then the file should be reachable from app in LocalFolder.

Stream localFolder = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync("sources.json");

string json;

using (StreamReader streamReader = new StreamReader(localFolder))
{
    json = streamReader.ReadToEnd();
}

return JsonConvert.DeserializeObject<List<SourceDefinition>>(json);

The error it is returning is:

Error: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)


Solution

  • just try File.OpenRead - you're propably trying to open from AppData which is a special path within thr userprofile (%localappdata% in this case)