I have difficulties to implement loading a savegame file in my Windows Phone project. I use EasyStorage to load and save files. I get the following exception when I try to load after saving a file.
An exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll but was not handled in user code
I'm sure that I get this exception because saving is not finished when I try to load the file. But how can I solve the problem? I want to load the file after it was created. The file needs to be created when you start the game the first time.
What is the best way to solve the problem?
public void LoadingData()
{
if (saveDevice.IsReady == true)
{
if ((saveDevice.FileExists("TestContainer", "Data" + ".txt")) == false)
{
// save a file asynchronously. this will trigger IsBusy to return true
// for the duration of the save process.
saveDevice.SaveAsync(
"TestContainer",
"Data" + ".txt",
stream =>
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(10 + ";" + 2 + ";" + 2);
int Start = -50;
int Offset = 150;
int Row = 150;
for (int i = 0; i <= 5 - 1; i++)
{
Start += Offset;
writer.WriteLine("Image" + (i + 1).ToString() + ";" + Start + ";" + Row + ";" + false);
}
}
});
saveDevice.Load(
"TestContainer",
"Data" + ".txt",
stream =>
{
using (StreamReader reader = new StreamReader(stream))
{
strs = reader.ReadLine().Split(';');
game1.Levels = int.Parse(strs[0]);
game1.LevelProgress = int.Parse(strs[1]);
game1.CurrentLevel = int.Parse(strs[2]);
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
}
});
}
else
{
saveDevice.Load(
"TestContainer",
"Data" + ".txt",
stream =>
{
using (StreamReader reader = new StreamReader(stream))
{
strs = reader.ReadLine().Split(';');
game1.Levels = int.Parse(strs[0]);
game1.LevelProgress = int.Parse(strs[1]);
game1.CurrentLevel = int.Parse(strs[2]);
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
strs = reader.ReadLine().Split(';');
game1.levellisttexstring.Add(strs[0]);
game1.levellistvec.Add(new Vector2(int.Parse(strs[1]), int.Parse(strs[2])));
game1.levellist.Add(bool.Parse(strs[3]));
}
});
}
}
}