问题
What is wrong? I am getting this error "Operation not permitted on IsolatedStorageFileStream" in this line:
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("folder\\" + fileName, FileMode.Create))
First time runs ok, but not the second time.
string nameFile= e.UserState.ToString();
Stream stream = e.Result;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!myIsolatedStorage.DirectoryExists("folder"))
{
myIsolatedStorage.CreateDirectory("folder");
infoTextBlock.Text = "'folder' created...";
}
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("folder\\" + fileName, FileMode.Create))
{
stream.CopyTo(fileStream);
}
}
UPDATED
Answering the questions:
Exception:
[System.IO.IsolatedStorage.IsolatedStorageException] = {"Operation not permitted on IsolatedStorageFileStream."}
StackTrace:
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf)
at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, IsolatedStorageFile isf)
at NameApp.Backup.client_DownloadImatgesCompleted(Object sender, LiveDownloadCompletedEventArgs e)
at Microsoft.Live.LiveConnectClient.<>c__DisplayClass6.<RaiseDownloadCompletedEvent>b__4()
at Microsoft.Live.SynchronizationContextWrapper.<>c__DisplayClass2.<Post>b__0(Object state)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
I put breakpoint where I am using (fileStream) and these are the filenames:
1st run:
Filenames:
- controlPic.txt
- PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg
- PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg
- PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg
2nd run:
Filenames:
- controlPic.txt
here crash, but the variable "NameFile" contains the correct names:
- PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg
- PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg
- PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg
UPDATED 2
I have seen that the problem happens after upload files. I have seen that I am getting exception when I try to open, delete, overwrite, etc. files that have previously been uploaded ...
I am using this code:
private void UploadFilePictures()
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
this.client.UploadCompleted
+= new EventHandler<LiveOperationCompletedEventArgs>(ISFileImatges_UploadCompleted);
foreach (string fileName in myIsolatedStorage.GetFileNames("folder\\*.jpg"))
{
fileStream = myIsolatedStorage.OpenFile("folder\\" + fileName, FileMode.Open, FileAccess.Read);
this.client.UploadAsync(skyCarpetaImatges_ID, fileName, true, fileStream, null);
}
}
}
private void ISFileImatges_UploadCompleted(object sender, LiveOperationCompletedEventArgs args)
{
fileStream.Close();
fileStream.Dispose()
this.client.UploadCompleted -= new EventHandler<LiveOperationCompletedEventArgs>(ISFileImatges_UploadCompleted);
}
SOLUTION:
SEE COMMENTS IN MARKED SOLUTION
回答1:
Try this:
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = new IsolatedStorageFileStream("folder\\" + fileName,FileMode.Create,FileAccess.Write,myIsolatedStorage ))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(data);
writer.Close();
}
stream.Close();
}
}
I hope it helps.
来源:https://stackoverflow.com/questions/12625910/getting-operation-not-permitted-on-isolatedstoragefilestream-saving-jpg