Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a problem with isolated storage.

This is my code:

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;

the mistake : Operation not permitted on IsolatedStorageFileStream. in

using (IsolatedStorageFileStream isoStream = 
        isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
358 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...