2010-11-03 104 views
1

我在Silverlight中构建了Windows Phone 7应用程序。我在使用IsolatedStorageFile时遇到困难。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()但留下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 

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

回答

0

乍一看,它听起来像你的流数据没有被刷新到磁盘。

您可能认为using块会在执行流式传输时执行刷新。不过,我发现并非总是如此,有时最好在最后强制Flush()

我记得最近在一个代码库中,我们收到了一个来自Microsoft团队的端口到WP7,他们强迫Flush。我最初质疑它,认为Dispose应该处理,但因为它是工作,我们在一个简短的截止日期,我没有进一步调查。

给它去,看看会发生什么... :)

+0

我试过了,并且它仍然不起作用(见上文)。 – 2010-11-05 13:58:09

0

您是否尝试过显式调用

writer.Close() 

,而不是依靠writer.Dispose()