C# BinaryFormatter Deserialization Issues

Hello Everyone,

I ran into one issue lately. I was working on a (test) Save and Load menu. Something like you can find in RTS game. When the game started I gathered all the files with curtain extension inside the folder and tried to opened them all one by one to then create UI list. Everything was working fine, but then I tried on purpose to create new text file with the same extension inside that folder. The thing I want to do is to check if the file is a valid game file before including it to the list. If the text file is empty I simply checked the file length and if it’s zero I pass that file, but if it’s not an empty text file I always get this error when I try to Deserialize it:

EndOfStreamException: Failed to read past end of stream.
System.IO.BinaryReader.FillBuffer (Int32 numBytes) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/BinaryReader.cs:119)

Can you like try to Deserialize the file and store the result (if it succeeded or not) into the bool variable?

I’m not sure how to solve this issue. Does anyone has some advice for me?

Thank you.

I ended up using ‘try/catch’ statement as suggested by the user #lordofduct in the Unity forum to check if the Deserialization was successful and return false if not.

Answer:
https://forum.unity.com/threads/c-binaryformatter-deserialization-issues.538655/