如何在Inno Setup中自动卸载以前安装的版本?

问题描述 投票:0回答:13

我正在使用 Inno Setup 创建安装程序。

我希望安装程序自动卸载以前安装的版本,而不是覆盖它。我怎样才能做到这一点?

installation inno-setup
13个回答
121
投票

我使用过以下内容。我不确定这是最简单的方法,但它确实有效。

这使用了

{#emit SetupSetting("AppId")}
,它依赖于 Inno Setup 预处理器。如果您不使用它,请直接剪切并粘贴您的App ID。

[Code]

/////////////////////////////////////////////////////////////////////
function GetUninstallString(): String;
var
  sUnInstPath: String;
  sUnInstallString: String;
begin
  sUnInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');
  sUnInstallString := '';
  if not RegQueryStringValue(HKLM, sUnInstPath, 'UninstallString', sUnInstallString) then
    RegQueryStringValue(HKCU, sUnInstPath, 'UninstallString', sUnInstallString);
  Result := sUnInstallString;
end;


/////////////////////////////////////////////////////////////////////
function IsUpgrade(): Boolean;
begin
  Result := (GetUninstallString() <> '');
end;


/////////////////////////////////////////////////////////////////////
function UnInstallOldVersion(): Integer;
var
  sUnInstallString: String;
  iResultCode: Integer;
begin
// Return Values:
// 1 - uninstall string is empty
// 2 - error executing the UnInstallString
// 3 - successfully executed the UnInstallString

  // default return value
  Result := 0;

  // get the uninstall string of the old app
  sUnInstallString := GetUninstallString();
  if sUnInstallString <> '' then begin
    sUnInstallString := RemoveQuotes(sUnInstallString);
    if Exec(sUnInstallString, '/SILENT /NORESTART /SUPPRESSMSGBOXES','', SW_HIDE, ewWaitUntilTerminated, iResultCode) then
      Result := 3
    else
      Result := 2;
  end else
    Result := 1;
end;

/////////////////////////////////////////////////////////////////////
procedure CurStepChanged(CurStep: TSetupStep);
begin
  if (CurStep=ssInstall) then
  begin
    if (IsUpgrade()) then
    begin
      UnInstallOldVersion();
    end;
  end;
end;

替代品

另请参阅 这篇博文“Inno Setup Script Sample for Version Comparison”,它更进一步,读取任何以前安装的版本的版本号,并将该版本号与当前安装包的版本号进行比较。


30
投票

您应该能够根据 AppId(即您在

AppID
部分中用于
[Setup]
的值)从注册表中读取卸载字符串。它可以在
Software\Microsoft\Windows\CurrentVersion\Uninstall\{AppId}\
下找到(可以是
HKLM
HKCU
,因此最好检查两者),其中
{AppId}
应替换为您使用的实际值。查找
UninstallString
QuietUninstallString
值并使用
Exec
函数从
InitializeSetup()
事件函数运行它。


10
投票

如果您“只是想删除旧图标”(因为您的图标已更改/更新),您可以使用此:

; attempt to remove previous versions' icons
[InstallDelete]
Type: filesandordirs; Name: {group}\*;

这是“在安装开始时”运行的,因此基本上会删除旧图标,并且在完全完成后,您的新图标仍将安装在那里。

我只是在每次安装时执行此操作,“以防万一发生任何变化”图标(无论如何都会重新安装)。


7
投票

使用 Inno Setup 时,没有理由卸载以前的版本,除非该版本是由不同的安装程序安装的。否则升级会自动处理。


4
投票

这是基于 Craig McQueen 的答案的简化版本

const
    UninstallRegisterPath = 'Software\Microsoft\Windows\CurrentVersion\Uninstall\' + '{#emit SetupSetting("AppName")}' + '_is1';

function GetUninstallerPath(): String;
begin
    result := '';
    if (not RegQueryStringValue(HKLM, UninstallRegisterPath, 'UninstallString', result)) then
        RegQueryStringValue(HKCU, UninstallRegisterPath, 'UninstallString', result);
end;

procedure UninstallOldVersion();
var
    UninstallerPath: String;
    ResultCode: Integer;
begin
    UninstallerPath := GetUninstallerPath();
    if (UninstallerPath <> '') then begin
        Exec(UninstallerPath, '/VERYSILENT /NORESTART /SUPPRESSMSGBOXES', '', SW_HIDE, ewWaitUntilTerminated, ResultCode);
    end;
end;

procedure CurStepChanged(CurStep: TSetupStep);
begin
    if (CurStep = ssInstall) then
    begin
        UninstallOldVersion();
    end;
end;

注意:就我而言,我使用

AppName
而不是
AppId


2
投票

Craig McQueen 提供的答案是完全可行的。不过,我会添加这些评论:

  • {#emit SetupSetting("AppId")}
    代码对我不起作用,所以我只需添加我的应用程序ID。
  • 我不想执行我的卸载程序,因为我有一个 INI 配置文件存储在 AppData/ 文件夹中,该文件已被卸载程序删除,并且我不希望在安装新版本时删除它。因此,我对 Craig McQueen 提供的代码进行了一些修改,以在检索其路径后删除安装程序的目录。

所以,关于 Craig McQueen 的代码,变化是:

  • 检索
    InstallLocation
    键而不是
    UninstallString
    键。
  • 使用
    DelTree
    函数代替
    Exec(sUnInstallString, ...)

1
投票

对于使用上面建议的

GetUninstallString()
CurStepChanged()
内强制卸载并存在磁盘缓存问题的任何人,请参阅下面的相关解决方案,该解决方案实际上在卸载后等待一段时间才能删除卸载程序exe!

inno-setup 的磁盘缓存问题?


1
投票

对于那些感兴趣的人,我为 Inno Setup 6 及更高版本编写了一个 DLL,它提供了支持自动卸载的简单机制。

DLL 提供了一种方法来检测您要安装的软件包是否已经安装(通过

AppId
),并根据安装的版本决定是否要自动卸载它(例如,您可能想自动卸载如果用户正在降级)。

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


0
投票

您可以在[code]部分执行卸载程序。您必须弄清楚如何获取现有卸载程序的路径。为了简单起见,当我安装应用程序时,我添加了一个指向包含卸载程序的文件夹的注册表字符串值,然后只需在 InitializeWizard 回调中执行卸载程序即可。

请记住,Inno 安装程序卸载程序名称均采用 uninsnnn.exe 形式,您需要在代码中考虑到这一点。


0
投票

我编辑了@Crain Mc-Queen 代码,我认为这个代码更好,因为不需要在不同的项目中修改:

[Code]
function GetNumber(var temp: String): Integer;
var
  part: String;
  pos1: Integer;
begin
  if Length(temp) = 0 then
  begin
    Result := -1;
    Exit;
  end;
    pos1 := Pos('.', temp);
    if (pos1 = 0) then
    begin
      Result := StrToInt(temp);
    temp := '';
    end
    else
    begin
    part := Copy(temp, 1, pos1 - 1);
      temp := Copy(temp, pos1 + 1, Length(temp));
      Result := StrToInt(part);
    end;
end;

function CompareInner(var temp1, temp2: String): Integer;
var
  num1, num2: Integer;
begin
    num1 := GetNumber(temp1);
  num2 := GetNumber(temp2);
  if (num1 = -1) or (num2 = -1) then
  begin
    Result := 0;
    Exit;
  end;
      if (num1 > num2) then
      begin
        Result := 1;
      end
      else if (num1 < num2) then
      begin
        Result := -1;
      end
      else
      begin
        Result := CompareInner(temp1, temp2);
      end;
end;

function CompareVersion(str1, str2: String): Integer;
var
  temp1, temp2: String;
begin
    temp1 := str1;
    temp2 := str2;
    Result := CompareInner(temp1, temp2);
end;

function InitializeSetup(): Boolean;
var
  oldVersion: String;
  uninstaller: String;
  ErrorCode: Integer;
  vCurID      :String;
  vCurAppName :String;
begin
  vCurID:= '{#SetupSetting("AppId")}';
  vCurAppName:= '{#SetupSetting("AppName")}';
  //remove first "{" of ID
  vCurID:= Copy(vCurID, 2, Length(vCurID) - 1);
  //
  if RegKeyExists(HKEY_LOCAL_MACHINE,
    'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' + vCurID + '_is1') then
  begin
    RegQueryStringValue(HKEY_LOCAL_MACHINE,
      'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' + vCurID + '_is1',
      'DisplayVersion', oldVersion);
    if (CompareVersion(oldVersion, '{#SetupSetting("AppVersion")}') < 0) then      
    begin
      if MsgBox('Version ' + oldVersion + ' of ' + vCurAppName + ' is already installed. Continue to use this old version?',
        mbConfirmation, MB_YESNO) = IDYES then
      begin
        Result := False;
      end
      else
      begin
          RegQueryStringValue(HKEY_LOCAL_MACHINE,
            'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\' + vCurID + '_is1',
            'UninstallString', uninstaller);
          ShellExec('runas', uninstaller, '/SILENT', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
          Result := True;
      end;
    end
    else
    begin
      MsgBox('Version ' + oldVersion + ' of ' + vCurAppName + ' is already installed. This installer will exit.',
        mbInformation, MB_OK);
      Result := False;
    end;
  end
  else
  begin
    Result := True;
  end;
end;

-2
投票

我一定是错过了什么。 在删除旧安装之前,new 文件将被复制到目标目录。 然后卸载程序会删除它们并删除目录。


-8
投票

不要使用[Run]部分,而是使用[UninstallRun]。 事实上,[运行]下的程序是在安装后执行的,导致安装后立即卸载程序:-| 相反,[UninstallRun] 部分会在安装之前进行评估。


-9
投票
http://news.jrsoftware.org/news/innosetup/msg55323.html

在InitializeSetup()函数中,当用户提示卸载旧版本后,您可以调用“MSIEXEC /x {您的程序ID}”

© www.soinside.com 2019 - 2024. All rights reserved.