运行时如何自我更新应用程序

I have a self updating standalone application in windows. The current update process goes as:

  1. Main app downloads file from server and save it to disk with a temporary name.
  2. Main app executes a secondary app with parameters.
  3. Main app exits.
  4. Secondary app renames temporary file to overwrite main app.exe.
  5. Secondary app executes main app.
  6. Secondary app exits.

The above works. But I'm wondering if its possible to do this without using a secondary app? Particularly with Golang.

The executable file is locked while running. However you can rename the file even though you can't delete it. So download the new app to a temporary file, make your app rename the app.exe to app-old.exe and then rename the temp file to app.exe. Also delete app-old.exe before downloading beginning the rename craze.

No. Windows locks writing to or deleting an executable file or DLL while there are any processes running it.

See for example this SO question - Locking Executing Files: Windows does, Linux doesn't. Why?

If it's OK to run an out-of-date version of the application and you only need it updated for the next time that it is run (skipping the step Secondary app executes main app. of restarting the application immediately) then see Sami Sallinen's answer for renaming the running EXE and DLLs.