0
votes

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(); 
}
1
Have you tried creating a smaller file? It could be that there's not enough space for the file. If you need more space you have to request it.ChrisF

1 Answers

0
votes

I think the problem might be due to the filestream being in use as the method call 'savegameStorage.CreateFile(FileName); returns a ref to an IsolatedStorageFileStream to the file that was created and then your SaveData(); continues to try and open the recently created file which still has the stream in use.

If you want to keep with the static calls with no params, I would create something along the lines of SaveNewData(); and instead of calling the open method call the CreateFile. Something like below.

public static void SaveNewData() 
{ 
    IsolatedStorageFile savegameStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
    IsolatedStorageFileStream fs = savegameStorage.CreateFile(Filename);
    //Continue with existing code.
}

Hope that helps.