Update current application by replace the EXE and delete old one
I worked on a small update project to update my current application which is simple it work as following,
1 - I download the new MyApp.exe to a temp.exe
2 - rename the MyApp.exe to old
3 - rename the temp.exe to MyApp.exe
4 - delete the old file
all this steps goes fine except the last step which not delete the old file i don't know why
here the simple code of this process
QString appName = QFileInfo(QCoreApplication::applicationFilePath()).fileName(); QFile realapp(appName); realapp.rename("old"); realapp.close(); QFile tempapp("temp"); tempapp.rename(appName); tempapp.close(); QFile removeOld("old"); removeOld.setPermissions(QFile::ReadOther|QFile::WriteOther); removeOld.remove(); removeOld.close();
I want to delete the old file to save the space of it and it no need to be found as it take a space for nothing.
I tried even to give permission to the folder as it may need it and nothing solves it, I run the application throw a release version and throw the QT debug but the problem still found the old file not deleted.
so what I did wrong? and how to solve it?
Thanks in advance.
Are you doing all this from within the old exe? doesn't windoze prohibit deleting of a file that is currently open?
Indeed, @Kent-Dorfman I think you're right (2 .exe files are needed.
Note that Windows does allow renaming while the .exe is open/running, so that step #1: rename() to "old" works fine. But the .exe is still running, so step #3: remove() "old" will always fail :-(
So why not remove the old when the new one is started?
@Christian-Ehrlicher Good idea, the update could then be done say this way:
toss the current step #3, instead do
QProcess::startDetached(appname); // if that worked (returned true) qApp->quit(); // will unlock "old" for deletion
then at every start of the program remove("old"), or, if that's not to your liking (and if you accept some debris in the directory until the next update) remove("old") as step #0 (so that step #1 always works).