Write out the state file before updating the registry.

This should reduce the window in which pressing Ctrl-C causes you to
need to list keys again next time.