windows-phone-7 - 获取 "Operation not permitted on IsolatedStorageFileStream"保存 jpg

标签 windows-phone-7 isolatedstorage

出了什么问题?我在这一行中收到此错误“Operation not allowed onisolatedStorageFileStream”:

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

已更新

回答问题:

异常(exception):

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

希望对您有所帮助。

关于windows-phone-7 - 获取 "Operation not permitted on IsolatedStorageFileStream"保存 jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625910/

相关文章:

windows-8 - 为什么我无法访问 System.IO 命名空间中的isolatedStorage?

xml - Windows Phone 7 - 加载一个大的 xml 文件

c# - 使用 UWP 将文件写入“下载”文件夹会进入独立存储吗?

windows-phone-7 - 导航到新页面而不将当前页面放回堆栈?

windows-phone-7 - 全景背景图像淡入动画有问题吗?

c# - 获取列表框Windows Phone 7的选定值

c# - 非 Metro 应用程序中的 StorageFile Async 使用

c# - Listpicker 错误 SelectedItem 必须始终设置为有效值

silverlight - Windows Phone 7 TextBlock TextWrapping 在列表框中不被认可

silverlight - 当我尝试获取值时,IsolatedStorageSettings 会抛出IsolatedStorageFileStream