Question

I have an application (WinForms C#) in which I would like to copy some files from a "Downloads" directory into the ProgramFiles directory. Essentially I am trying to update some (or all) of the currently running program dlls and executables by rolling my own automatic updater.

So I have the application automatically checking for updates and downloading updates into the ProgramData\MyApplication directory. The problem is, I need to overwrite all the files for the application in its corresponding ProgramFiles\MyApplication directory, including the .exe which is currently running. Any idea on how this could be performed?

Was it helpful?

Solution

Writing a self-updating application correctly is not a trivial task. Most of the problems you mention in your question, such as files in use etc.

From trial and error I find that the best approach (roughly) is to:

  1. Create a system-wide event in the application using wait handles that can listen for requests to shutdown from another process.
  2. Package the application via an MSI installer.
  3. Use a bootstrap application in %TEMP% folder to send the signal to the first application, and start the MSI installer.
  4. The MSI will then handle all the other hard work of actually updating the application for you, dealing with file locks etc.

Unfortunately, the problem with self-updating apps is that unless you design all of this to work with v1 (and test it before release with a fake v1.1), then you'll find that you need features in the old version to help do the update to the new version.

If an MSI installer isn't an option, then a bootstrap application running in a temporary directory that can do the swapping, process termination etc. is a good way to start.

Depending on your application design, you may also need to ensure that the self-update process elevates itself on Windows Vista and later so that it can write to directories such as %PROGRAMFILES% to do the file writes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top