Operation not permitted on IsolatedStorageFileStream. error

前端 未结 4 1630
说谎
说谎 2020-12-06 18:40

I have a problem with isolated storage.

This is my code:

List data = new List();

using (IsolatedStorageFile isoStore = 
           


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 18:53

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

提交回复
热议问题