System.IO.IsolatedStorage.IsolatedStorageException: Operation not permitted on IsolatedStorageFileStream in Windows Phone 8 App

I was recently try out some samples using Windows Phone 8 SDK and one such sample was using Isolated Storage . After trying it out for some time , i ended up getting the below error when creating a new file in the isolated storage.

“An exception of type ‘System.IO.IsolatedStorage.IsolatedStorageException’ occurred in mscorlib.ni.dll but was not handled in user code

Additional information: Operation not permitted on IsolatedStorageFileStream.”

image

For example , when try to execute the below code , you might get the above error.

private void Button_Click(object sender, RoutedEventArgs e)
{
    // Create a file in Isolated Storage
    IsolatedStorageFile storage= IsolatedStorageFile.GetUserStoreForApplication();
    storage.CreateFile("developerpublish\test.txt");
}

One of the reasons for the above error was the invalid directory . The directory “developerpublish” does not exist where we are trying to create the text file under this directory . Before creating the file under the directory(that does not exist) , first we need to create the Directory.

Below is the modified code to handle this.

private void Button_Click(object sender, RoutedEventArgs e)
{
   // Create a file in Isolated Storage
   IsolatedStorageFile storage= IsolatedStorageFile.GetUserStoreForApplication();
   storage.CreateDirectory("developerpublish");
   if (storage.FileExists("developerpublish\test.txt"))
     storage.DeleteFile("developerpublish\test.txt");
   storage.CreateFile("developerpublish\test.txt");
}

Leave a Reply