Windows 7 Phone – Loading from Isolated Storage

In my previous post (Saving to isolated storage) I demonstrated a simple example of saving a serialised json file to the Windows 7 Phone isolated storage, in this post I’m going to show a simple example of retrieving a file out of isolated storage simlar to my approach in the Windows 7 phone database Rapid Repository.

Here we go…

Load isolated storage file
  1. public class LoadFileFromIsolatedStorage
  2. {
  3.     private string myDirectory = "MyDirectory";
  4.  
  5.     public string LoadFile(string fileName)
  6.     {
  7.         try
  8.         {
  9.             using (var store = IsolatedStorageFile.GetUserStoreForApplication())
  10.             {
  11.                 string filePath = string.Format("{0}.json", Path.Combine(myDirectory, fileName));
  12.  
  13.                 using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, store))
  14.                 {
  15.                     using (TextReader reader = new StreamReader(fileStream))
  16.                     {
  17.                         return reader.ReadLine();
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.         catch (IsolatedStorageException exception)
  23.         {
  24.             throw exception;
  25.         }
  26.     }
  27. }

As you can see, a store variable is set to the application user store and get the file path.

We then open a file stream and use a text reader to read the json file which is then returned as a string.

I hope this is useful, if you would like to see a more ‘real world’ example – you can view the source of the Rapid Repository FileManager class here.

Kind Regards,

Sean McAlinden.

No Comments