使用存储在注册表中的版本号检测并卸载 Inno Setup 中的旧版本应用程序

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

我有一个安装程序在 Windows 注册表中写入这一行

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

考虑到{#MyAppVersion}是在程序安装的时候定义写的

#define MyAppVersion "2.7"

我一直在更新安装程序,这就是为什么有些人有旧的安装,他们更新时,将冲突的旧文件合并在一起,以免卸载以前的版本,有一些方法可以读取此注册表在开始安装之前。

我读过以前的帖子,但它们只适用于程序的“GUID”或“appID”,尝试修改一些代码行但我什么也得不到,如果有人能帮助我,我提前谢谢你,对不起我的英语我使用翻译我来自拉丁美洲

How to detect old installation and offer removal?

Inno Setup: How to automatically uninstall previous installed version?

使用 RegQueryStringValue function and CompareVersion function from (您的问题),您可以:

#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;

只需从您在问题中链接的一些答案中插入卸载代码。


但请注意,您不需要编写自己的版本注册表值。标准卸载注册表项中有DisplayVersionVersionMajorVersionMinor

对于任何感兴趣的人,我为 Inno Setup 编写了一个 Windows DLL,如果需要的话,它提供了一种简单的方法来执行此操作:

https://github.com/Bill-Stewart/UninsIS

DLL 需要 Inno Setup 6 或更新版本,因为 1) 它只是 Unicode 并且 2) 它需要指定 64 位与 32 位以及管理与非管理安装模式的参数。

DLL 可以检测当前安装的版本并判断安装的版本是旧版本、新版本还是相同版本,以便您可以执行仅在降级时自动卸载等操作(例如)。