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

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.

More Reading

Post navigation

  • 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.

  • So, a class like this one would work? I am very new to programming, so sorry if these questions sound basic.

    using System.IO.IsolatedStorage;
    using System.IO;

    namespace GameStateManagement
    {
    static class Storage
    {
    static public void LoadContent()
    {
    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();

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

  • So, like this:

    namespace GameStateManagement
    {
    static class Storage
    {
    static public void LoadContent(string file)
    {
    IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); // grab the storage
    FileStream stream = store.OpenFile(“file”, FileMode.Create); // Open a file in Create mode
    BinaryWriter writer = new BinaryWriter(stream);
    float myvar = 5.0f;
    writer.Write(“doodle”);
    writer.Write(myvar);
    writer.Close();

    if (store.FileExists(“file”)) // Check if file exists
    {
    IsolatedStorageFileStream save = new IsolatedStorageFileStream(“file”, FileMode.Open, store);
    BinaryReader reader = new BinaryReader(save);
    string mystring = reader.ReadString();
    float myfloat = (float)reader.ReadSingle();
    reader.Close();
    }
    }
    }
    }

    If so, thanks.

    My last question is how do I allow a button in another class to be touched to load the saved file?

  • For iphone there are apps that help load and save files on the phone. What about WP7? Do they intend to create similar programs-software?

  • Thanks for this description.

    Now this application run right,but when I close the emulator and start new run (F5)
    the data was not found.

    If it is happen because I use emulator,and when I use the device on my hand the application will work right?

    Thanks.

  • I have a question and I do not know if you can assist me or not,

    Now,I can access web server such as Currency converter “http://www.webservicex.net/CurrencyConvertor.asmx?WSDL” using its xml code,but if I want use website not web server

    how can I access it? I mean any web site such as facebook ,goal.com ,maps.google

    Thanks.

  • I have a question. Is it possible to load/save file using user interface? I mean, on button’s click open some window where files are located.

  • Hi, how to save an image example .bmp image from stream ? I try to read an .bmp image file with StreamResourceInfo like this :
    StreamResourceInfo sri = null;
    Uri Uri = new Uri(“Images/lena.bmp”, UriKind.Relative);
    sri = Application.GetResourceStream(Uri);
    i make a change with some byte in stream and write in to other stream (out stream).
    How to make a new image from out stream, please?

  • Don’t forget to Dispose all the IDisposable objects properly! The preferable way would be to wrap it all in using-blocks:

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

Leave a Reply

Your email address will not be published. Required fields are marked *