Detect and uninstall old version of application in Inno Setup using its version number stored in registry

后端 未结 1 1428
情话喂你
情话喂你 2021-01-03 17:01

I have an installer that write this line in the Windows registry

[Registry]
Root: "HKCU"; Subkey: "SOFTWARE\\W117GAMER"; ValueType: string         


        
1条回答
  •  花落未央
    2021-01-03 17:40

    With use of RegQueryStringValue function and CompareVersion function from Compare version strings in Inno Setup (your question), you can do:

    #define MyAppVersion "2.6"
    
    [Code]
    
    function InitializeSetup(): Boolean;
    var
      InstalledVersion: string;
      VersionDiff: Integer;
    begin
      Result := True;
      if not RegQueryStringValue(
               HKCU, 'Software\My Program', 'DSVersionL4D2', InstalledVersion) then
      begin
        Log('No installed version detected');
      end
        else
      begin
        Log(Format('Found installed version %s', [InstalledVersion]));
        VersionDiff := CompareVersion(InstalledVersion, '{#MyAppVersion}');
        if VersionDiff < 0 then
        begin
          MsgBox(
            Format('You have an old version %s installed, will uninstall it.', [
              InstalledVersion]),
            mbInformation, MB_OK);
          { Uninstall old version here }
        end
          else
        if VersionDiff = 0 then
        begin
          MsgBox(
            'You have this version installed already, cancelling installation.',
            mbInformation, MB_OK);
          Result := False;
        end
          else
        begin
          MsgBox(
            Format(
              'You have newer version %s installed already, cancelling installation.', [
              InstalledVersion]),
            mbInformation, MB_OK);
          Result := False;
        end;
      end;
    end;
    

    Just plug-in an uninstallation code from some of the answers you have linked in your question.


    Though note that you do not need to write your own version registry value. There are DisplayVersion, VersionMajor and VersionMinor in the stnadard uninstall registry key.

    0 讨论(0)
提交回复
热议问题