我正在 Silverlight 中构建 Windows Phone 7 应用程序。我在使用 IsolatedStorage
时遇到困难。
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
if (!storage.FileExists(STORIES_FILE))
{
storage.CreateFile(STORIES_FILE);
}
string contents;
// fails here
using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.Open))
{
using (StreamReader reader = new StreamReader(stream))
{
contents = reader.ReadToEnd();
}
}
异常(exception)情况是:
"Operation not permitted on IsolatedStorageFileStream."
System.Exception {System.IO.IsolatedStorage.IsolatedStorageException}
我在这里做错了什么? MSDN says当隔离存储被删除或禁用时抛出此异常。那会发生吗?我在模拟器上遇到了这个问题。
更新:这似乎只发生在我第一次在模拟器上运行应用程序时。应用崩溃后,我再次在模拟器上运行,并没有出现这个问题。
更新 2:使用 FileMode.OpenOrCreate
而不是 FileMode.Open
似乎已经解决了问题。
最佳答案
第一次运行应用程序时,该文件不存在,请试试这个:
using (IsolatedStorageFileStream stream = storage.OpenFile(STORIES_FILE, FileMode.OpenOrCreate))
{
using (StreamReader reader = new StreamReader(stream))
{
contents = reader.ReadToEnd();
}
}
关于c# - .NET:独立存储异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4090123/