不允许对 IndependentStorageFileStream 进行操作.错误 [英] Operation not permitted on IsolatedStorageFileStream. error

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

问题描述

我在使用隔离存储时遇到问题.

I have a problem with isolated storage.

这是我的代码:

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)
   {
        //...
   }
}

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

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