The problem was my filename. I added the date to my filename, the ":" and the "/" signs caused the trouble. I feel pathetic...
Operation not permitted on IsolatedStorageFileStream when I try to use a StreamWriter
-
02-06-2022 - |
Question
I have a problem, when I try to use this code:
using (IsolatedStorageFile myISF = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
object _readLock = new object();
lock (_readLock)
{
var myFS = myISF.CreateFile(reply_FileName.Replace(" ", "_"));
StreamWriter mySW = new StreamWriter(myFS);
mySW.WriteLine(jsonToSave);
mySW.Close();
}
}
catch (IsolatedStorageException ex) { MessageBox.Show(ex.Message); }
}
I have already try StreamWriter
with using too, but the problem wasn't resolved. I have 2 page, on first page I use:
using (myFS = new IsolatedStorageFileStream(Forms_path + form_ID + ".json",
FileMode.Create, FileAccess.Write, myISF))
{
using (StreamWriter mySW = new StreamWriter(myFS))
{
mySW.WriteLine(json);
mySW.Close();
}
myFS.Dispose();
}
myFS.Close();
It's working good. Just the second code is faulty. I tried a lot of variations, but neither works good.
EDIT: when the problem occurred, I get this message in my Output window:
A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll
EDIT2: The problem was my filename. I added the date to my filename, the ":" and the "/" signs caused the trouble. I feel pathetic...
La solution 2
Autres conseils
You create the lock object every time the method called so it will not block other threads, thus the exception. The _readLock variable should be a static field in your class.