Over a million developers have joined DZone.

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

· Mobile Zone

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;
writer.Write("something");
writer.Write(myvar);
writer.Close();

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();
       reader.Close();
}

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.

Topics:

Published at DZone with permission of David Amador, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}