无法读取JSON文件

编程入门 行业动态 更新时间:2024-10-24 12:33:16
无法读取JSON文件 - FileNotFoundException(Cannot read JSON file - FileNotFoundException)

我正在关注此页面上的教程http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-22-Storing-and-Retrieving-Serializedvals因为我希望我的应用程序将数据存储在JSON文件中,然后将其读回。 关于本教程的两件事:

我按下写按钮 - 工作正常,然后按下阅读按钮,它也可以工作,但是,我关闭win手机8.1模拟器,再次打开它,按下阅读按钮,我得到了An exception of type 'System.IO.FileNotFoundException'exception! 为什么会这样,我应该从以前运行应用程序的磁盘上获得该文件? 或者当我关闭模拟器时它会被删除吗? 我也在磁盘上查找指定的文件,但找不到它! 有帮助吗?

private const string JSONFILENAME = "data.json";

private async Task readJsonAsync()
{

    string content = String.Empty;

    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
    using (StreamReader reader = new StreamReader(myStream))
    {
        content = await reader.ReadToEndAsync();
    }

    resultTextBlock.Text = content;
}


private async Task writeJsonAsync()
{   // Notice that the write is ALMOST identical ... except for the serializer.

    var myCars = buildObjectGraph();

    var serializer = new DataContractJsonSerializer(typeof(List<Car>));
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                  JSONFILENAME,
                  CreationCollisionOption.ReplaceExisting))
    {
        serializer.WriteObject(stream, myCars);
    }

    resultTextBlock.Text = "Write succeeded";
}

I was following tutorial on this page http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-22-Storing-and-Retrieving-Serialized-Data because I want my app to store data in JSON file and then read it back. 2 things about the tutorial:

I press write button - works fine, then press read button and it also works, however, I close down win phone 8.1 emulator, open it again, press read button and I got the An exception of type 'System.IO.FileNotFoundException'exception! Why is that, I should have the file already on disk from previously running the app ? Or is does it get erased when I close down emulator ? Also I looked for the specified file on disk and cannot find it ! Any help ?

private const string JSONFILENAME = "data.json";

private async Task readJsonAsync()
{

    string content = String.Empty;

    var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
    using (StreamReader reader = new StreamReader(myStream))
    {
        content = await reader.ReadToEndAsync();
    }

    resultTextBlock.Text = content;
}


private async Task writeJsonAsync()
{   // Notice that the write is ALMOST identical ... except for the serializer.

    var myCars = buildObjectGraph();

    var serializer = new DataContractJsonSerializer(typeof(List<Car>));
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                  JSONFILENAME,
                  CreationCollisionOption.ReplaceExisting))
    {
        serializer.WriteObject(stream, myCars);
    }

    resultTextBlock.Text = "Write succeeded";
}

                

最满意答案

当您关闭模拟器时,其状态不会被保留,这意味着当您重新启动模拟器时,您测试的应用程序不存在。 因此,当您再次打开模拟器时,VS将进行新安装。

当您重建项目时也会发生同样的情况。 然后,VS将从模拟器中删除该应用程序并从头开始重新安装。 这反过来也会导致您的JSON文件丢失。

如果要确保保留数据,请不要关闭模拟器,只使用VS中的Build(VS会不时决定重建您的应用程序)。

为了更准确地测试您的应用程序,我建议您查看Windows Phone Power Tools( http://wptools.codeplex.com/ )。 在那里,您可以明确选择安装或更新给定的XAP包。

When you close the Emulator its state is is not preserved, meaning that the apps that you tested are not there when you restart the emulator. Therefore, VS will make a new install when you open up the emulator again.

The same happens when you do a rebuild of your project. Then, VS will remove the app from the emulator and reinstall it from scratch. This in turn will also result in loosing your JSON file.

When you want to make sure that your data is preserved then don't close the emulator and just use Build from within VS (VS decides from time to time to rebuild your app though).

On order to test your app more properly I suggest you have a look at the Windows Phone Power Tools (http://wptools.codeplex.com/). There you can explicitly choose to install or update a given XAP package.

更多推荐

本文发布于:2023-07-06 07:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!