【问题标题】:Getting "Operation not permitted on IsolatedStorageFileStream" saving jpg获取“在独立存储文件流上不允许操作”保存 jpg
【发布时间】:2012-09-27 16:26:08
【问题描述】:

怎么了?我在此行中收到此错误“Operation not allowed on IsolatedStorageFileStream”:

using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("folder\\" + fileName, FileMode.Create))

第一次运行正常,但第二次不行。

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);                                
    }
}

更新

回答问题:

例外:

[System.IO.IsolatedStorage.IsolatedStorageException] = {"IsolatedStorageFileStream 上不允许操作。"}

堆栈跟踪:

   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)

我在我使用的地方放置了断点(fileStream),这些是文件名:

第一次运行:

文件名:

  • controlPic.txt
  • PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg
  • PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg
  • PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg

第二次运行:

文件名:

  • controlPic.txt

此处崩溃,但变量“NameFile”包含正确的名称:

  • PhotoChooser-c197865c-cbe6-4202-a95f-e4eeb16da943.jpg
  • PhotoChooser-d1d0c947-664f-425b-aaa2-85b17a4a73ec.jpg
  • PhotoChooser-4ce1b884-8d7f-444e-925d-2ca2ae2b3b7c.jpg

更新 2

我发现问题是在上传文件后发生的。我发现当我尝试打开、删除、覆盖等之前上传的文件时遇到异常...

我正在使用此代码:

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);
}

解决方案:

在标记的解决方案中查看评论

【问题讨论】:

  • 另外,对于这两种情况(第一次运行和第二次运行),请中断您的 using (fileStream) 语句并告诉我们 nameFile 是什么。
  • 我已经更新了我的问题来回答你的问题。

标签: windows-phone-7 isolatedstorage


【解决方案1】:

试试这个:

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();
      }
}

希望对你有帮助。

【讨论】:

  • 你好米兰。我已经尝试过,但我遇到了同样的错误。我已经更新了我的问题。
  • 更新了代码。这对我有用。如果这不起作用,请尝试使用 IS tool 查看运行时 IS 内部实际发生的情况。
  • 这可能是因为没有关闭流。现在尝试更新的代码。看看有没有帮助。运行代码后的其他内容。试试上面的 IS 工具链接,看看你的文件夹中实际保存了什么。
  • 嗨米兰:我正在复制图像(.jpg),除了一个(是一个 txt 文件),我不能使用“writer.WriteLine”。但是使用“using(IsolatedStorageFileStream fileStream = etc etc)”就不需要“close()”了吧?
  • answer here 和你做的差不多。看看有没有帮助。我到处看到人们都关闭了溪流。看起来像它的推荐。如果它不会引起任何问题,你也应该关闭它。
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多