Close the temporary file before removing it

This is needed for Windows.