[英]Difficulty with IsolatedStorageFile
我正在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()
调用中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
我已经进行了几次测试。 我敢肯定,只有第一项才能被读回。
乍一看,听起来好像您的流数据没有被刷新到磁盘上。
你可能在想的是, using
时,块将执行冲洗Disposes
流。 但是我发现情况并非总是如此, 有时最好在最后强制使用Flush()
。
我记得最近在我们从Microsoft团队收到的移植到WP7的代码库中,他们迫使Flush
。 我最初质疑它,认为是Dispose
应该处理该问题,但是由于它正在运行,而且我们的截止日期很短,因此我不会对其进行进一步调查。
放手,看看会发生什么... :)
您是否尝试过明确呼叫
writer.Close()
而不是依赖writer.Dispose()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.