Over a million developers have joined DZone.

Using Isolated Storage to save/load files on Windows Phone 7

DZone's Guide to

Using Isolated Storage to save/load files on Windows Phone 7

· Mobile Zone ·
Free Resource

I’m seeing a lot of forum threads with people asking how to save/load files on Windows Phone 7, well for XNA 4 in general.
You can use IsolatedStorage for that

using System.IO.IsolatedStorage;

Both save and load can be done by creating a IsolatedStorageFile, I then use a Filestream and write with a binaryWriter

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); // grab the storage
FileStream stream = store.OpenFile("test.txt", FileMode.Create); // Open a file in Create mode
BinaryWriter writer = new BinaryWriter(stream);
float myvar = 5.0f;

For loading is pretty much the same thing:

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists("test.txt")) // Check if file exists
       IsolatedStorageFileStream save = new IsolatedStorageFileStream("test.txt", FileMode.Open, store);
       BinaryReader reader = new BinaryReader(save);
       string mystring = reader.ReadString();
       float myfloat  = (float)reader.ReadSingle();

Simple right? I really don’t know if this is the best way but I’ve tested on both the emulator and a real device and it works.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}