问题
I have managed to get my application checking for updates with a WebService on my server and It will see if any update is there to be made. But the real problem I am facing right now is that I have no idea how to download the newer version of .exe of my application and replace the current one I'm running.
In conclusion I need to know how to download the newer version of .exe of my application and replace the current one I'm running.
回答1:
Well actually you need to create a separate process to handle the update steps. I have once developed a desktop application with the ability to receive updates form a web server and i did that by creating a different process that roles as the main entry point to the main application.
it starts the main application process while it keeps running to check for updates. Then if any updates available it download them to a temp folder after that it notifies the main application before closing it then it commits the new updates and restart the main application again.
But if you want to stick with one executable and one process. I guess you can't replace any executable while it is running that's not allowed by the OS itself.
回答2:
You'll need to create a separate bootstrap application to do the work for you, as otherwise the app with be running so the OS will prevent you from overwriting it.
回答3:
Well actually there is way. I discovered it years ago.
You cannot delete the current executing .exe program file. But you can move it.
So, I download the update file from my web server to an /updates/myprogram.exe
Then I 1) File.Delete any previous myprogram.bak file 2) File.Move the myprogram.exe to myprogram.bak. 3) File.Copy the /updates/myprogram.exe to /myprogram.exe location. 4) ask the user if they want to restart, calling Application.Restart()
Voila. Problem resolved in your main program. No need to a boot program.
来源:https://stackoverflow.com/questions/22891580/update-c-sharp-application-replace-exe-file