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
- public class LoadFileFromIsolatedStorage
- {
- private string myDirectory = "MyDirectory";
- public string LoadFile(string fileName)
- {
- try
- {
- using (var store = IsolatedStorageFile.GetUserStoreForApplication())
- {
- string filePath = string.Format("{0}.json", Path.Combine(myDirectory, fileName));
- using (IsolatedStorageFileStream fileStream = new IsolatedStorageFileStream(filePath, FileMode.Open, store))
- {
- using (TextReader reader = new StreamReader(fileStream))
- {
- return reader.ReadLine();
- }
- }
- }
- }
- catch (IsolatedStorageException exception)
- {
- throw exception;
- }
- }
- }
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.