title: Strategies For Replacing Program Executable in Windows
body: <p>I have a Windows program that needs to update itself from a server automatically.</p>
  
  <p>It is already able to transfer files from the server and update + validate things like DLL plugin files before loading them.</p>
  
  <p>However, this program also needs to update itself.  There are probably a few different ways this could be done, and the most obvious one I've seen from various online game clients is creating an "auto patcher" that downloads and then runs the client executable.  That introduces the problem of having to update the autopatcher, so if there is a more elegant solution I'd like to hear about it.</p>
  
  <p>I have to imagine there is a way to download what will be the new executable file as a temporary file, let's say "client.exe.tmp" and then launch a separate process that waits for the original client.exe to exit and then renames/copies the new file over the top of it.</p>
  
  <p>Has anyone done this sort of thing before successfully and what method did you use to get it to work?</p>