1

我正在 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 是我在一次调用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()but leave 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

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

4

2 回答 2

0

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

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

我记得最近在我们从微软团队收到的一个代码库中,他们强制将Flush. 我最初质疑它,认为Dispose应该处理它,但是由于它正在工作并且我们的截止日期很短,我没有进一步调查它。

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

于 2010-11-04T09:04:13.110 回答
0

您是否尝试过显式调用

writer.Close()

而不是依赖 writer.Dispose()

于 2010-11-25T21:38:52.223 回答