我只是在尝试使用一些基本的独立存储功能,并且在此行的 SaveData 函数中不断收到无效操作:
IsolatedStorageFileStream fs = savegameStorage.OpenFile(Filename, FileMode.Open);
这是我正在使用的两个主要功能。
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();
}