0

I use this function to save my game state into file with serialization:

public void SaveForX86 ()
{
    UpdateGameState();
    try
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream fs = File.Create(Application.persistentDataPath + Helper.GAME_DATA_FILE_NAME);

        GameData data = new GameData();
        data.experience = experience;
        data.score = score;
        data.winPercent = winPercent;
        data.tasksSolved = tasksSolved;
        data.correct = correct;
        data.additions = additions;
        data.subtractions = subtractions;
        data.multiplications = multiplications;
        data.divisions = divisions;
        data.useAddition = useAddition;
        data.useSubtraction = useSubtraction;
        data.useMultiplication = useMultiplication;
        data.useDivision = useDivision;
        data.minRange = minRange;
        data.maxRange = maxRange;
        data.longestChain = longestChain;
        data.useIncrementalRange = useIncrementalRange;
        data.gameStateDirty = gameStateDirty;
        data.longestTaskInSeconds = longestTaskInSeconds;
        data.overallTimeInSeconds = overallTimeInSeconds;
        data.operandsSign = operandsSign;
        data.difficulty = difficulty;

        bf.Serialize(fs, data);
        fs.Close();
    }
    catch (Exception ex)
    {
        Debug.Log(ex.Message);
    }
}

I would like to check whether the file was created. How can I do that in this case?

4
  • So I leave the question as it is or delete it? Commented Feb 26, 2016 at 12:51
  • You can´t delete it as you have answers. Commented Feb 26, 2016 at 13:00
  • @vlad please click delete to save a lot of time for moderators. i gave you the full answer in the dupe question you made. Commented Feb 26, 2016 at 13:04
  • I can't, it has answers :) Commented Feb 26, 2016 at 13:24

2 Answers 2

1

Use File.Exists:

if (File.Exists(Application.persistentDataPath + Helper.GAME_DATA_FILE_NAME))
{
  ....
}
Sign up to request clarification or add additional context in comments.

Comments

1

You don´t need to as if serialization fails (e.g. because of an IO-error) an exception should be thrown. So if none is thrown your file should have been created.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.