2010-11-03 105 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()