不允许对 IsolatedStorageFileStream 进行操作。使用 MvvmCross 文件插件时
Operation not permitted on IsolatedStorageFileStream. when using MvvmCross file plugin
我有一个用于 Windows Phone 的 IMvxFileStore 实现实例注入到我的服务中。
假设我想将设置存储在位于 appsettings\userprofiles\profile1.txt
的文件中
使用文件插件,我首先调用 API EnsureFolder 存在,传入我的配置文件设置文件 appsettings\userprofiles\profile1.txt 的完整路径确保此文件夹已创建并且确实存在。
出于理智考虑,我检查以确保已使用 FolderExist API 创建了该文件夹。至少现在这总是 returns 正确。
代码如下所示:
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 将内容写入文件时,传入从上述方法返回的文件名和一些字符串,如下所示
try
{
_fileStore.WriteFile(filename, content);
}
catch (Exception ex)
{
Logger.Error(ex);
}
,我得到以下异常:
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:
我的开发环境设置如下:
- 表面临 3
- Visual Studio 2013 社区
- Windows Phone 8.1 SDK 和模拟器
- MvvmCross 3.0.0.4。 (是的,它是旧的,将在 MVP 之后更新)。
在我深入研究 MvvmCross 代码库之前,有人知道我在这里可能做错了什么吗?
将项目升级到 .NET 4.5 并升级到 MvvmCross 3.5.1,但仍然 运行 遇到同样的问题。虽然我因为升级耽误了几天时间,但我很高兴它已经结束了。
我 运行 遇到的问题与我打电话给
的事实有关
_fileStore.EnsureFolderExists(filename);
并传入文件的完整路径。在幕后,MvvmCross 正在调用
IsolatedStorageFile.CreateDirectory(filename);
似乎正在创建一个具有相同文件名的目录。因此将 "images\image1.png" 传递给上面的 API 似乎创建了一个目录或保存一些与 "images\images.png".
相关的 IO 资源
当您尝试使用
写入文件时
_fileStore.WriteFile(filename, content);
MvvmCross 正在尝试使用
创建文件流
var fileStream = new IsolatedStorageFileStream(path, FileMode.Create, isf))
异常就在这里抛出。
修复是为了确保您只传递给 IMvxFileStore.EnsureFolderExists API 有问题的文件夹。在本例中为 "images"。然后你将文件的相对路径传递给 IMvxFileStore.WriteFile,包括文件名,例如 "images\image1.png",你就可以开始了。
固定代码如下所示:
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;
}
我有一个用于 Windows Phone 的 IMvxFileStore 实现实例注入到我的服务中。
假设我想将设置存储在位于 appsettings\userprofiles\profile1.txt
的文件中使用文件插件,我首先调用 API EnsureFolder 存在,传入我的配置文件设置文件 appsettings\userprofiles\profile1.txt 的完整路径确保此文件夹已创建并且确实存在。
出于理智考虑,我检查以确保已使用 FolderExist API 创建了该文件夹。至少现在这总是 returns 正确。
代码如下所示:
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 将内容写入文件时,传入从上述方法返回的文件名和一些字符串,如下所示
try
{
_fileStore.WriteFile(filename, content);
}
catch (Exception ex)
{
Logger.Error(ex);
}
,我得到以下异常:
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:
我的开发环境设置如下: - 表面临 3 - Visual Studio 2013 社区 - Windows Phone 8.1 SDK 和模拟器 - MvvmCross 3.0.0.4。 (是的,它是旧的,将在 MVP 之后更新)。
在我深入研究 MvvmCross 代码库之前,有人知道我在这里可能做错了什么吗?
将项目升级到 .NET 4.5 并升级到 MvvmCross 3.5.1,但仍然 运行 遇到同样的问题。虽然我因为升级耽误了几天时间,但我很高兴它已经结束了。
我 运行 遇到的问题与我打电话给
的事实有关 _fileStore.EnsureFolderExists(filename);
并传入文件的完整路径。在幕后,MvvmCross 正在调用
IsolatedStorageFile.CreateDirectory(filename);
似乎正在创建一个具有相同文件名的目录。因此将 "images\image1.png" 传递给上面的 API 似乎创建了一个目录或保存一些与 "images\images.png".
相关的 IO 资源当您尝试使用
写入文件时_fileStore.WriteFile(filename, content);
MvvmCross 正在尝试使用
创建文件流var fileStream = new IsolatedStorageFileStream(path, FileMode.Create, isf))
异常就在这里抛出。
修复是为了确保您只传递给 IMvxFileStore.EnsureFolderExists API 有问题的文件夹。在本例中为 "images"。然后你将文件的相对路径传递给 IMvxFileStore.WriteFile,包括文件名,例如 "images\image1.png",你就可以开始了。
固定代码如下所示:
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;
}