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

断了今生、忘了曾经 提交于 2020-05-14 05:19:49

问题


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

[Registry]
Root: "HKCU"; Subkey: "SOFTWARE\W117GAMER"; ValueType: string; ValueName: "DSVersionL4D2"; ValueData: "{#MyAppVersion}"

taking into account that {#MyAppVersion} is defined and written when the program is installed

#define MyAppVersion "2.7"

I am constantly updating the installer, which is why some people have old installations, and when they update, old files that conflict are combined, so as not to uninstall the previous version, there is some way to read this registry before starting the installation.

  • First case: if the user has an old version, uninstall it and install the new version
  • Second case: if the user has the same version, tell them that they
    already have the latest version installed and cancel installation
  • Third case: if the user has a version installed and is going to
    install an old version, tell them that they already have the newest version of the program

I read previous posts but they only work with "GUID" or "appID" of the program, try to modify some lines of code but I could not get anything, if someone could help me I thank you in advance, sorry for my English I use a translator I am from Latin America

How to detect old installation and offer removal?

Inno Setup: How to automatically uninstall previous installed version?


回答1:


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\Martin Prikryl\WinSCP 2',
           '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.



来源:https://stackoverflow.com/questions/60821439/detect-and-uninstall-old-version-of-application-in-inno-setup-using-its-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!