Cloud Training (Online)




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");
}
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.