我对隔离存储有疑问。
这是我的代码:
List<Notes> data = new List<Notes>();
using (IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream =
isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate))
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Notes>));
data = (List<Notes>)serializer.Deserialize(isoStream);
}
}
data.Add(new Notes() { Note = "hai", DT = "Friday" });
return data;
错误:不允许对IsolatedStorageFileStream进行操作。在
using (IsolatedStorageFileStream isoStream =
isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate))
最佳答案
当您同时多次执行该代码块时,通常会发生这种情况。您最终将文件锁定了。因此,必须确保在构造函数中包括FileAccess和FileShare模式,如下所示:
using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage)
{
//...
}
如果要在其他人正在读取时写入文件,则需要像这样同步锁定:
private readonly object _readLock = new object();
lock(_readLock)
{
using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage)
{
//...
}
}
关于c# - 在IsolatedStorageFileStream上不允许进行该操作。错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8415979/