c# Windows Phone cannot find json file

213 views Asked by At

I'm trying to read data from json file in .NET 4.5 for Windows Phone app. After pressing button the exception appears saying:

System.IO.FileNotFoundException (Exception from HRESULT: 0x80070002)

My code:

public static async Task ReadFile()
{
    StorageFolder local = Windows.ApplicationModel.Package.Current.InstalledLocation;

    if (local != null)
    {
        var file = await local.OpenStreamForReadAsync("bazaDanych.json");

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

Here's my view of Solution Explorer:
enter image description here

1

There are 1 answers

2
Anton Sizikov On BEST ANSWER

You're not copying your file to the local storage.

Put your json file under the Assets folder, make sure that it's properties says "Content" and "Copy Always"

On the first launch you should read the json from the package

var filename = "Assets/BazaDanych.json";
var sFile = await StorageFile.GetFileFromPathAsync(filename);
var fileStream = await sFile.OpenStreamForReadAsync();

And store into the local storage.

There is an example for Windows 8 (which is more or less the same)

Related question.