Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

visual studio 2013 - Operation not permitted on IsolatedStorageFileStream. when using MvvmCross file plugin

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

Let say I want to store settings in a file located at appsettingsuserprofilesprofile1.txt

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

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

The code looks like this:

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

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

, I get the following exception:

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:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

IsolatedStorageFile.CreateDirectory(filename);

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

When you attempt to write to file using

_fileStore.WriteFile(filename, content);

MvvmCross is attempting to create a file stream using

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

and the exception is thrown right here.

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 "imagesimage1.png" and you are good to go.

Fixed code looks like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...