Question

Description:

The code below is the simplest code I could write which causes the failure. I've also tried: putting the CreateFile and MoveFile in different using statements, putting them in different xaml pages, moving the file into a subdirectory with a new filename, moving it into a subdirectory with the same filename. They all throw the same exception. CopyFile throws the same exception in all circumstances.

Question is--what incredibly simple thing am I not accounting for?

  1. Open a new Silverlight for Windows Phone 7 project targeting Windows Phone 7.1.
  2. Open App.xaml.cs.
  3. Paste the following lines of code into Application_Launching:

    using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
    {
        isf.CreateFile("hello.txt");
        isf.MoveFile("hello.txt", "hi.txt");
    }
  4. Click start debugging, targeting emulator or device.

Expected: creates a file named "hello.txt", then (effectively) renames "hello.txt" to "hi.txt".
Actual: throws exception below.

System.IO.IsolatedStorage.IsolatedStorageException was unhandled
  Message=An error occurred while accessing IsolatedStorage.
  StackTrace:
       at System.IO.IsolatedStorage.IsolatedStorageFile.MoveFile(String sourceFileName, String destinationFileName)
       at PhoneApp4.App.Application_Launching(Object sender, LaunchingEventArgs e)
       at Microsoft.Phone.Shell.PhoneApplicationService.FireLaunching()
       at Microsoft.Phone.Execution.NativeEmInterop.FireOnLaunching()
Was it helpful?

Solution

You should call Close after you create the file.

IsolatedStorageFileStream helloFile = store.CreateFile("hello.txt");
helloFile.Close();
isf.MoveFile("hello.txt", "hi.txt");

OTHER TIPS

I was just having the same issue, but the solution is simple:
The target file must not exists, delete it before the moving. Make sure the target file is not open anywhere before deleting.
The source file must not be open anywhere.

if (_isolatedStorage.FileExists(targetPath))
{
    _isolatedStorage.DeleteFile(targetPath);
}
_isolatedStorage.MoveFile(sourcePath, targetPath);

Perfectly execute this piece of code

  var file = await ApplicationData.Current.LocalFolder.GetFileAsync(oldName);
  await file.RenameAsync(newName);

MBen, your answer is not correct. Calling Close on the file does not fix this error. I am seeing the exact same error as well even though I call "Close" before MoveFile.

edit Ok just figured out the problem I was having - if you try to call MoveFile when the destinationFile already exists, it throws an Exception. You have to delete the destinationFile first before moving your sourceFile to it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top