在IsolatedStorageFileStream上不允许进行该操作.使用MvvmCross文件插件时 [英] Operation not permitted on IsolatedStorageFileStream. when using MvvmCross file plugin

查看:120
本文介绍了在IsolatedStorageFileStream上不允许进行该操作.使用MvvmCross文件插件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于Windows Phone的IMvxFileStore实现的实例注入到我的服务中.

I have an instance of IMvxFileStore implementation for for Windows Phone injected into my service.

假设我要将设置存储在位于appsettings \ userprofiles \ profile1.txt的文件中

Let say I want to store settings in a file located at appsettings\userprofiles\profile1.txt

使用文件插件,我首先调用API EnsureFolder 存在,并将其完整路径传递到我的个人资料设置文件appsettings \ userprofiles \ profile1.txt中,以确保已创建并执行此文件夹存在.

Using the file plugin, I first call the API EnsureFolder exists, passing in the full path to my profile settings file appsettings\userprofiles\profile1.txt to ensure that this folder has been created and does exist.

为简便起见,我检查以确保已使用 FolderExist API创建了该文件夹.至少现在总是返回true.

Just for sanity, I check to ensure that the folder has been created using the FolderExist API. This always returns true atleast for now.

代码如下:

  private string GetStorageItemFileName(string filename)
        {
            Guard.ThrowIfNull(string.IsNullOrWhiteSpace(BaseDirectory), Messages.RepositorBackingStoreIsNull);
            filename = _fileStore.PathCombine(BaseDirectory, filename); 

            _fileStore.EnsureFolderExists(filename);    
            if (_fileStore.FolderExists(filename))
            {
                // what do do????
            }

            return filename;
        }

但是,当我尝试使用 WriteToFile API将内容写入文件时,传入上述方法返回的文件名和一些字符串,如下所示

However, when I attempt to write content to the file using WriteToFile API, passing in the file name returned from the method above and some string, as follows

 try
            {
                _fileStore.WriteFile(filename, content);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }    

,出现以下异常:

System.IO.IsolatedStorage.IsolatedStorageException被捕获
HResult = -2146233264消息=不允许进行操作 IsolatedStorageFileStream.来源= mscorlib StackTrace: 在System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(字符串路径, FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize, IsolatedStorageFile isf) 在System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(字符串路径, FileMode模式,IsolatedStorageFile isf) 在Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore.WriteFileCommon(String 路径,Action`1 streamAction) 在Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore.WriteFile(String 路径,字符串内容) 在TrackuTransit.Core.Services.DataStore.StorageItemFileRepository.Put(String 文件名,StorageItem数据)InnerException:

System.IO.IsolatedStorage.IsolatedStorageException was caught
HResult=-2146233264 Message=Operation not permitted on IsolatedStorageFileStream. Source=mscorlib 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, IsolatedStorageFile isf) at Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore.WriteFileCommon(String path, Action`1 streamAction) at Cirrious.MvvmCross.Plugins.File.WindowsPhone.MvxIsolatedStorageFileStore.WriteFile(String path, String contents) at TrackuTransit.Core.Services.DataStore.StorageItemFileRepository.Put(String filename, StorageItem data) InnerException:

我的开发环境设置如下: -Surface Pro 3 -Visual Studio 2013社区 -Windows Phone 8.1 SDK和模拟器 -MvvmCross 3.0.0.4. (是的,它很旧,会在MVP之后更新).

My dev environment is setup as follows: - Surface Pro 3 - Visual Studio 2013 Community - Windows Phone 8.1 SDK and Simulator - MvvmCross 3.0.0.4. (yes,, it is old and will be updated after MVP).

在我深入研究MvvmCross代码库之前,有人对我在这里可能做错的事情有想法吗?

Before I go digging into the MvvmCross code base, anyone with ideas on what I could be doing wrong here?

推荐答案

将项目升级到.NET 4.5,还升级到MvvmCross 3.5.1,仍然遇到相同的问题.尽管由于升级我损失了几天的时间,但我很高兴一切都结束了.

Upgraded the project to .NET 4.5 and also upgraded to MvvmCross 3.5.1 and still ran into the same problem. Although I lost a couple of days due from the upgrade, I am glad it is over with.

我在这里遇到的问题与我正在打电话的事实有关

The problem I am running into here has to do with the fact that I am calling

 _fileStore.EnsureFolderExists(filename);  

,然后传递文件的完整路径.在幕后,MvvmCross正在呼叫

and passing in the full path to the file. Underneath the scenes, MvvmCross is calling

IsolatedStorageFile.CreateDirectory(filename);

似乎正在创建具有相同文件名的目录.因此,将"images \ image1.png"传递给上述API似乎会创建一个目录或保存一些与"images \ images.png"相关的IO资源.

which appears to be creating a directory with the same file name. So passing in "images\image1.png" to the above API appears to create a directory or hold some IO resource related to "images\images.png".

当您尝试使用

_fileStore.WriteFile(filename, content);

MvvmCross尝试使用

MvvmCross is attempting to create a file stream using

var fileStream = new IsolatedStorageFileStream(path, FileMode.Create, isf))

在这里抛出异常.

修复此问题是为了确保您仅将相关文件夹传递给IMvxFileStore.EnsureFolderExists API.在这种情况下,它就是图像".然后,将包含文件名(例如"images \ image1.png")的文件的相对路径传递给IMvxFileStore.WriteFile,就可以了.

Fix is to ensure you are passing to IMvxFileStore.EnsureFolderExists API just the folder in question. In this case it is "images". Then you pass to IMvxFileStore.WriteFile the relative path to the file including the file name such as "images\image1.png" and you are good to go.

固定的代码如下:

private string GetStorageItemFileName(string filename)
        {
            Guard.ThrowIfNull(string.IsNullOrWhiteSpace(BaseDirectory), Messages.RepositorBackingStoreIsNull);
            filename = _fileStore.PathCombine(BaseDirectory, filename); 

           // assumption is that filename does not contain directory information
            _fileStore.EnsureFolderExists(BaseDirectory);    

            return filename;
        }

这篇关于在IsolatedStorageFileStream上不允许进行该操作.使用MvvmCross文件插件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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