Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!
Windows Phone Zone is brought to you in partnership with:

I've been a game developer since 2005. After working 3 years in the game industry I've decided to go indie and create a little development studio called Different Pixel. We've debuted our first game Vizati on June 2010 David is a DZone MVB and is not an employee of DZone and has posted 17 posts at DZone. You can read more from them at their website. View Full User Profile

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

07.19.2011
Email
Views: 3931
  • submit to reddit
It's so easy to get up and running with your first Windows Phone app!  Just pick up the FREE toolkit from Microsoft, register at AppHub, and start checking out some fundamental tutorials.

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.

References
Published at DZone with permission of David Amador, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

The Windows Phone Microzone, which is supported by Microsoft, is your one-stop-shop for news, tutorials, perspectives, and research on the mobile platform that is making waves in smartphone ecosystem.

Comments

Gagan Peter replied on Sat, 2012/04/14 - 5:17am

Where exactly does this text go? Unloadcontent for save? Loadcontent for load? I have been trying to implement load and save, but have found it very difficult.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.