Search code examples
c#windows-phone-7isolatedstorageisolatedstoragefile

IsolatedStorageFile's MoveFile() method throws IsolatedStorageException


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

Solution

  • You should call Close after you create the file.

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