I'm just trying to use some basic Isolated Storage functionality and I keep getting an invalid operation in my SaveData function on this line:
IsolatedStorageFileStream fs = savegameStorage.OpenFile(Filename, FileMode.Open);
Here is the two main functions I'm working with.
public static void OpenData()
{
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
if (savegameStorage.FileExists(Filename))
{
using (IsolatedStorageFileStream fs = savegameStorage.OpenFile(Filename, System.IO.FileMode.Open))
{
if (fs != null)
{
if (fs.Length == 0)
{
fs.Close();
fs.Dispose();
SaveData();
return;
}
// Reload the saved high-score data.
byte[] saveBytes = new byte[6 * (50)];
int count = fs.Read(saveBytes, 0, (6 * 50) + 8);
ByteStream BS = new ByteStream(ByteStreamMode.Read, saveBytes, 0);
LevelData.LastStage = BS.ReadInt32();
LevelData.LastLevel = BS.ReadInt32();
for (int i = 0; i < 6; ++i)
for (int x = 0; x < 5; ++x)
for (int y = 0; y < 10; ++y)
LevelData.LevelComplete[i][x, y] = BS.ReadBool();
}
else
{
fs.Close();
fs.Dispose();
savegameStorage.CreateFile(Filename);
SaveData();
}
fs.Close();
}
}
else
{
SaveData();
}
}
public static void SaveData()
{
IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream fs = savegameStorage.OpenFile(Filename, FileMode.Open);
if (fs != null)
{
ByteStream BS = new ByteStream(ByteStreamMode.Write, null, 0);
BS.WriteInt32(LevelData.LastStage);
BS.WriteInt32(LevelData.LastLevel);
for (int i = 0; i < 6; ++i)
for (int x = 0; x < 5; ++x)
for (int y = 0; y < 10; ++y)
BS.WriteBool(LevelData.LevelComplete[i][x, y]);
fs.Write(BS.StreamBuffer, 0, BS.StreamBuffer.Length);
}
fs.Close();
}