在IsolatedStorageFileStream上不允许进行该操作。错误 [英] Operation not permitted on IsolatedStorageFileStream. error

查看:72
本文介绍了在IsolatedStorageFileStream上不允许进行该操作。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对隔离存储有疑问。

这是我的代码:

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上不允许进行操作。在

the mistake : Operation not permitted on IsolatedStorageFileStream. in

using (IsolatedStorageFileStream isoStream = 
        isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate))


推荐答案

通常在同时执行几次该代码块时发生。您最终锁定了文件。因此,必须确保像这样在构造函数中包括FileAccess和FileShare模式:

This usually happens when you execute that code block several times concurrently. You end up locking the file. So, you have to make sure you include FileAccess and FileShare modes in your constructor like this:

using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage)
{
//...
}

如果要在其他人正在读取的同时写入文件,则需要像这样同步锁定:

If you want to write to the file while others are reading, then you need to synchronize locking like this:

private readonly object _readLock = new object();

lock(_readLock)
{
   using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage)
   {
        //...
   }
}

这篇关于在IsolatedStorageFileStream上不允许进行该操作。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆