Operation not allowed on IsolStorageFileStream. mistake

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; 

error: operation not allowed on IsolStorageFileStream. in

 using (IsolatedStorageFileStream isoStream = isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate)) 
+8
c # windows-phone-7 silverlight isolatedstorage windows-phone-8
source share
4 answers

This usually happens when you execute this block of code several times at the same time. As a result, you will lock the file. So, you should make sure that the FileAccess and FileShare parameters are included in your constructor:

 using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage) { //... } 

If you want to write to a file while others are reading, you need to synchronize the lock as follows:

 private readonly object _readLock = new object(); lock(_readLock) { using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage) { //... } } 
+16
source share

Replace the internal using statement with the IsolStorageFileStream constructor:

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

Also, since you are reading from a file, I assume that you want FileMode to be Open, not OpenOrCreate.

And where the "data" is assigned, consider using

 serializer.Deserialize( isoStream ) as List<Notes> 

instead of this. See Point 3 in Effective C #, 2nd ed.

0
source share

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

0
source share

Operation not allowed on IsolStorageFileStream. An error occurred while moving a file from a shared file to a destination. His job

Add Namespaces

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

Create one target 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 the file name to the destination, add the folder name.

You can play media using the 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) { } 
-one
source share

All Articles