获取“不允许对独立存储文件流进行操作";保存jpg [英] Getting "Operation not permitted on IsolatedStorageFileStream" saving jpg

查看:23
本文介绍了获取“不允许对独立存储文件流进行操作";保存jpg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么了?我在此行中收到此错误在隔离存储文件流上不允许操作":

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

更新

回答问题:

例外:

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

[System.IO.IsolatedStorage.IsolatedStorageException] = {"Operation not permitted on 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"包含正确的名称:

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

我看到问题是在上传文件后发生的.我已经看到当我尝试打开、删除、覆盖等以前上传的文件时出现异常...

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 ...

我正在使用此代码:

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

推荐答案

试试这个:

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

希望能帮到你.

这篇关于获取“不允许对独立存储文件流进行操作";保存jpg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆