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

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

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.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda


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.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}