Problem loading file not saved in session

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;
		}       
	}

Finally found the answer on Google. It’s not the loading the file but the damn Serialization.
Unity apparently scrambles its assembly name on every compile or some such stupid behaviour.

public sealed class VersionDeserializationBinder : SerializationBinder
	{
	    public override Type BindToType( string assemblyName, string typeName )
	    {
	        if ( !string.IsNullOrEmpty( assemblyName )  !string.IsNullOrEmpty( typeName ) )
	        {
	            Type typeToDeserialize = null;
				String currentAssembly = Assembly.GetExecutingAssembly().FullName;

	            assemblyName = currentAssembly;   
	
	            // The following line of code returns the type.
	            typeToDeserialize = Type.GetType( String.Format( "{0}, {1}", typeName, assemblyName ) );
	
	            return typeToDeserialize;
	        }
	 
	        return null;
	    }
	}

Then whenever your using the BinaryFormatter set it up with the above binder

			BinaryFormatter formatter = new BinaryFormatter();  
			formatter.Binder = new VersionDeserializationBinder();