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.