Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update current application by replace the EXE and delete old one
Forum Updated to NodeBB v4.3 + New Features

Update current application by replace the EXE and delete old one

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 1.7k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AmrCoderA Offline
    AmrCoderA Offline
    AmrCoder
    wrote on last edited by
    #1

    Hello,

    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.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Are you doing all this from within the old exe? doesn't windoze prohibit deleting of a file that is currently open?

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      2
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #3

        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 :-(

        1 Reply Last reply
        2
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So why not remove the old when the new one is started?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          2
          • hskoglundH Offline
            hskoglundH Offline
            hskoglund
            wrote on last edited by
            #5

            @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).

            1 Reply Last reply
            4

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved