title: Getting "Operation not permitted on IsolatedStorageFileStream" saving jpg
body: <p>What is wrong? I am getting this error "Operation not permitted on IsolatedStorageFileStream" in this line:                 </p>
  
  <pre><code>using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("folder\\" + fileName, FileMode.Create))
  </code></pre>
  
  <p>First time runs ok, but not the second time.</p>
  
  <pre><code>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);                                
      }
  }
  </code></pre>
  
  <h2>UPDATED</h2>
  
  <p>Answering the questions:</p>
  
  <p><strong>Exception:</strong></p>
  
  <p>[System.IO.IsolatedStorage.IsolatedStorageException] = {"Operation not permitted on IsolatedStorageFileStream."}</p>
  
  <p><strong>StackTrace:</strong></p>
  
  <pre><code>   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.&lt;&gt;c__DisplayClass6.&lt;RaiseDownloadCompletedEvent&gt;b__4()
     at Microsoft.Live.SynchronizationContextWrapper.&lt;&gt;c__DisplayClass2.&lt;Post&gt;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&amp; stackMark)
     at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark&amp; 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&amp; pResult)
  </code></pre>
  
  <p><strong>I put breakpoint where I am using (fileStream) and these are the filenames:</strong></p>
  
  <p>1st run:</p>
  
  <p>Filenames:</p>
  
  <ul>
  <li>controlPic.txt</li>
  <li>PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg</li>
  <li>PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg</li>
  <li>PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg</li>
  </ul>
  
  <p>2nd run:</p>
  
  <p>Filenames:</p>
  
  <ul>
  <li>controlPic.txt</li>
  </ul>
  
  <p>here crash, but the variable "NameFile" contains the correct names:</p>
  
  <ul>
  <li>PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg</li>
  <li>PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg</li>
  <li>PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg</li>
  </ul>
  
  <h2>UPDATED 2</h2>
  
  <p>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 ...</p>
  
  <p>I am using this code:</p>
  
  <pre><code>private void UploadFilePictures()
  
  using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
  {
     this.client.UploadCompleted
                      += new EventHandler&lt;LiveOperationCompletedEventArgs&gt;(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&lt;LiveOperationCompletedEventArgs&gt;(ISFileImatges_UploadCompleted);
  }
  </code></pre>
  
  <h2>SOLUTION:</h2>
  
  <p>SEE COMMENTS IN MARKED SOLUTION </p>