Operation not permitted on IsolatedStorageFileStream. error

13,302

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)
   {
        //...
   }
}
Share:
13,302
yozawiratama
Author by

yozawiratama

Updated on June 22, 2022

Comments

  • yozawiratama
    yozawiratama almost 2 years

    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))
    
  • yozawiratama
    yozawiratama over 12 years
    as your suggestion i change to be : data = serializer.Deserializer(isoStream) as List<Notes> , but i got new problem There is an error in XML document (0, 0).
  • Visual Stuart
    Visual Stuart over 12 years
    That sounds like you have not previously written to this IsolatedStorageFile, is that correct? Either your application's business logic should guarantee that the file has been previously writtten to, or you should check for the file's existence, before attempting to read it. Write the file using a similar construct as you have for the file read, but calling Serialize instead of Deserialize, and using FileMode.OpenOrCreate.