Operation not permitted on IsolatedStorageFileStream. error
Asked Answered
U

4

8

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))
Unkind answered 7/12, 2011 at 13:21 Comment(0)
I
16

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)
   {
        //...
   }
}
Impetuous answered 7/7, 2012 at 0:12 Comment(0)
C
0

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.

Cortes answered 7/12, 2011 at 13:51 Comment(2)
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).Unkind
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.Cortes
T
0

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.

Thi answered 17/1, 2019 at 10:56 Comment(0)
P
-1

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)
  {
  }
Pansie answered 8/7, 2015 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.