8
votes

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))
4

4 Answers

16
votes

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

Replace the inner using statement with an IsolatedStorageFileStream constructor:

using ( var isoStream = new IsolatedStorageFileStream( "Notes.xml", FileMode.Open, isoStore ) )

Also, since you're reading from the file, I assume the FileMode you want is Open, not OpenOrCreate.

And where 'data' is assigned, consider using

serializer.Deserialize( isoStream ) as List<Notes>

instead. See Item 3 in Effective C#, 2nd Ed.

0
votes

In case of Silverlight it can also happen when the full path exceeds a certain character limit. I could not find any official reference for this, but as I have tested in on win10 and IE, it seems to be somewhere between 115 and 120 chars.

-1
votes

Operation not permitted on IsolatedStorageFileStream. error at the time of moving file from shared fileto destination. Its working

Add Namespaces

 using BackgroundProcess.Resources;
 using Microsoft.Phone.BackgroundTransfer;
 using System.IO.IsolatedStorage;

Create one destination directory in isolated storage

 BackgroundTransferRequest transfer;
 using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())

 {

      if (isoStore.GetDirectoryNames("DestinationFolder").Length == 0)
           isoStore.CreateDirectory("DestinationFolder");

      storage.MoveFile("/shared/transfers/xyzFileName.mp3", "DestinationFolder");

 }

or use

 isoStore.MoveFile(transfer.DownloadLocation.OriginalString, "DestinationFolder");

Instead of adding filename in destination add foldername.

You can play media by using following code

 try 
 {
      string isoFileName = "DestinationFolder//xyzFileName.mp3";

      StorageFile file = null;

      try
      {
           file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + isoFileName));
      }
      catch (Exception ex)
      {
      }
      if (file != null)
           await Windows.System.Launcher.LaunchFileAsync(file);
  }
  catch (Exception ex)
  {
  }