Hi All,
I’ve got some simple loading and saving code which works fine if I save the file, do some things then load it. If I stop the program however then start it again and try load I get the following error.
Any ideas? I’ve been googling for permission errors or similar. A separate block of code which lists the contents of a folder does correctly show the file. So the program can find it, and the filename is correct.
Code Below
public static void WriteSaveFile(string filename, object obj)
{
try
{
Stream fileStream = File.Open("Blueprints/" + filename, FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, obj);
fileStream.Close();
}
catch(Exception e)
{
Debug.LogError("SaveLoad.WriteSaveFile(): Failed to serialize object to a file " + filename + " (Reason: " + e.ToString() + ")");
}
}
public static object ReadSaveFile(string filename)
{
try
{
Stream fileStream = File.Open("Blueprints/" + filename, FileMode.Open, FileAccess.Read);
BinaryFormatter formatter = new BinaryFormatter();
object obj= formatter.Deserialize(fileStream);
fileStream.Close();
return obj;
}
catch(Exception e)
{
if(e is FileNotFoundException)
{
FileNotFoundException fError = e as FileNotFoundException;
Debug.LogError("SaveLoad.ReadSaveFile(): File not Found! [ " + filename + " -- " + fError.FileName + "] \n (Reason: " + e.ToString() + ")");
}
else
{
Debug.LogError("SaveLoad.ReadSaveFile(): Failed to desirialize a file [" + filename + "] (Reason: " + e.ToString() + ")");
}
return null;
}
}