Close settings files and save them correctly

Fixes #1911

Tested on Windows XP SP 3