Introduction
To store local data a on a Windows Phone 7 phone, the tool of choice is isolated storage. There are several efforts to create Windows Phone 7 databases, but in the end, these all run on top of isolated storage.
Isolated Storage is not new to Windows Phone, but has been around a while in the Silverlight world. And while it might sound fancy, isolated storage is just a clever way of saying “Write your data to text files.”
Isolated
Well, writing to text files covers the “storage” part of it at least. The “isolated” part of it just means that your application can’t see other application’s data and other applications can’t see your data. The data is isolated per application. If you need two applications that work with the same data, then that data can’t be local to either application. Instead, it should be in the cloud, which is a fancy way of saying “on the internet”. Once your data is shared through the cloud, as many applications as you want can all access the same data and even keep their own local copy in isolated storage if it makes sense.
How it’s done – writing to a file
Note: For a complete working reference, check out the source code for Gas Mileage for Windows Phone 7 on Codeplex.
To write to isolated storage, the first thing you’ll need is an instance of IsolatedStorageFile. This class represents the file system that you’ll be working with on the phone. It implements IDisposable, so be sure to wrap its usage in a “using” statement.
1: using (var store = IsolatedStorageFile.GetUserStoreForApplication())
The next step is to get a stream to write to. For example, to write to a file called “data.txt”, this code would get us started.
1: using (var store = IsolatedStorageFile.GetUserStoreForApplication())
2: using (var stream = new IsolatedStorageFileStream("data.txt",
3: FileMode.Create,
4: FileAccess.Write,
5: store))
6: {
7: // Write to our file
8: }
This creates a file stream that can be used for writing to the file “data.txt”. Setting the FileMode to Create means that the file will be created for us, even if it already exists. For all of the FileMode options, check out this MSDN page.
The final step is to serialize the data that we want to save. For this example, I’m using the XmlSerializer that you’ll find in the System.Xml.Serialization namespace. If you’re just saving text, then there is no need to bother with serialization – just write the text.
1: using (var store = IsolatedStorageFile.GetUserStoreForApplication())
2: using (var stream = new IsolatedStorageFileStream("data.txt",
3: FileMode.Create,
4: FileAccess.Write,
5: store))
6: {
7: var serializer = new XmlSerializer(typeof(Notebook));
8: serializer.Serialize(stream, notebook);
9: }
Reading from a file
Reading from a file is very similar.
1: using (var store = IsolatedStorageFile.GetUserStoreForApplication())
2: using (var stream = new IsolatedStorageFileStream("data.txt",
3: FileMode.OpenOrCreate,
4: FileAccess.Read,
5: store))
6: using (var reader = new StreamReader(stream))
7: {
8: return reader.EndOfStream
9: ? new Notebook()
10: : (Notebook) _serializer.Deserialize(reader);
11: }
This time, I set the FileMode to OpenOrCreate so that if the file does not exist it will be created for me. I also check to see if the reader is at the end of its stream before reading. If it is, this indicates the file is empty and has just been created. In this case, I return a new notebook rather than trying to deserialize an empty string of data.
Again, for the complete source code these examples are pulled from, check out Gas Mileage for Windows Phone 7 on Codeplex.
Best Practices & Quotas
Microsoft has published isolated storage best practices for the phone on MSDN. Here are some of the highlights.
- If you change your isolated storage format between versions of your application, it is up to you to upgrade or modify your files. In practical terms, this means that the newer version of your app will have to upgrade your isolated storage files the first time it runs since you have no control of the upgrade process.
- There is no limit to how much disk space a Windows Phone application can take up. Just like desktop development, if you want to be a jerk and take up all of the available disk space, you can. But this will not please your users, so don’t use more of their phone than you need to use.
Isolated Storage on the Emulator
The Windows Phone 7 emulator does not remember isolated storage data between runs. This means that your application will start with nothing in isolated storage when the emulator first fires up. If you want your isolated storage data around between deployments to the emulator, then just leave the emulator open. Visual Studio can easily re-deploy your application to the already-running emulator.
More Information