【问题标题】:Difficulty with IsolatedStorageFile隔离存储文件的困难
【发布时间】:2010-11-03 19:07:45
【问题描述】:

我正在 Silverlight 中构建一个 Windows Phone 7 应用程序。我在使用IsolatedStorageFile 时遇到了困难。

下面的方法应该是把一些数据写入文件:

    private static void writeToFile(IList<Story> stories)
    {
        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Append))
        {
            using (StreamWriter writer = new StreamWriter(stream))
            {
                StringBuilder toJson = new StringBuilder();

                IList<StoryJson> storyJsons = (from story in stories
                                               where !storageStories.Contains(story)
                                               select story.ToStoryJson()).ToList();

                writer.Write(JsonConvert.SerializeObject(storyJsons));
            }

        }

#if DEBUG
            StreamReader reader = new StreamReader(storage.OpenFile(STORIES_FILE, FileMode.Open));
            string contents = reader.ReadToEnd();
#endif
        }

最后的DEBUG 是让我检查数据是否正在写入。我已经验证是这样的。此方法被调用 6 次以上。每次都会追加更多的数据。

但是,当我去读取数据时,我得到的唯一 JSON 是我在 one 调用 writeToFile() 中编写的。这是我的阅读方法:

    private static IList<Story> storageStories;
    private static IList<Story> readFromStorage()
    {
        if (storageStories != null)
        {
            return storageStories;
        }

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();

        if (! storage.FileExists(STORIES_FILE))
        {
            storage.CreateFile(STORIES_FILE);
            storageStories = new List<Story>();
            return storageStories;
        }

        string contents;
        using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                contents = reader.ReadToEnd();
            }
        }

        JsonSerializer serializer = new JsonSerializer();
        storageStories = JArray.Parse(contents).Select(storyData => storyOfJson(serializer, storyData)).ToList();
        return storageStories;
    }

我在这里做错了什么?我是否错误地写入文件?我很确定唯一能够读回的数据来自第一次写入。

更新:我添加了两个Flush() 调用,但它崩溃了:

  private static void writeToFile(IList<Story> stories)
        {
            IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Append))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    StringBuilder toJson = new StringBuilder();

                    IList<StoryJson> storyJsons = (from story in stories
                                                   where !storageStories.Contains(story)
                                                   select story.ToStoryJson()).ToList();

                    writer.Write(JsonConvert.SerializeObject(storyJsons));
                    writer.Flush();
                }
                // FAILS
                // "Cannot access a closed file." {System.ObjectDisposedException}

                stream.Flush();
            }
        }

如果我注释掉stream.Flush() 但留下writer.Flush(),我也会遇到同样的问题。

更新 2:我添加了一些打印语句。看起来一切都在序列化:

Serializing for VID 43
Serializing for VID 17
Serializing for VID 6
Serializing for VID 33
Serializing for VID 4
Serializing for VID 5
Serializing for VID 3

但实际上只有第一组被回读:

Deserializing stories with vid: 43

我已经运行了几次测试。我很确定只有第一项被回读。

【问题讨论】:

    标签: c# .net silverlight windows-phone-7 isolatedstorage


    【解决方案1】:

    乍一看,您的流数据似乎没有被刷新到磁盘。

    您可能认为using 块将在Disposes 流时执行刷新。但是我发现情况并非总是如此,有时最好在最后强制使用Flush()

    我记得最近在我们从 Microsoft 团队收到的要移植到 WP7 的代码库中,他们强制使用 Flush。我最初对此提出质疑,认为 Dispose 应该处理这个问题,但由于它工作而且我们的截止日期很短,我没有进一步调查。

    放手,看看会发生什么... :)

    【讨论】:

      【解决方案2】:

      您是否尝试过显式调用

      writer.Close()
      

      而不是依赖于 writer.Dispose()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多