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

Vim*_*987 82 installer inno-setup

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

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

Cra*_*een 104

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

这种用途{#emit SetupSetting("AppId")}依赖于Inno Setup Preprocessor.如果您不使用它,请直接剪切并粘贴您的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;
Run Code Online (Sandbox Code Playgroud)

备择方案

另请参阅此博客文章"版本比较的Inno安装脚本示例",它更进一步,并读取任何以前安装的版本的版本号,并将该版本号与当前安装包的版本号进行比较.

  • 经过测试并确认可行.在我的解决方案中使用它. (2认同)
  • 感谢您参考我的博文.该帖子的完整示例可在此处找到,http://code.google.com/p/lextudio/source/browse/trunk/trunk/setup/CBC2Exe.iss (2认同)
  • 好的解决方案,工作正常.但是,它会在安装过程中打开一个窗口,显示"卸载[软件名称]".是否可以防止此窗口弹出?因为我的软件安装速度太快,安装窗口在卸载窗口之前关闭,看起来很奇怪...... (2认同)
  • @AndréSantaló使用/ VERYSILENT而不是/ SILENT (2认同)
  • ewWaitUntilTermminate 不起作用。卸载程序将自身复制到临时文件夹并从临时文件夹重新启动。 (2认同)

Oli*_*sen 25

在给定AppId(即您AppID[Setup]-section中使用的值)时,您应该能够从注册表中读取卸载字符串.它可以在下面找到Software\Microsoft\Windows\CurrentVersion\Uninstall\{AppId}\(可以是HKLM或者HKCU,最好检查两者){AppId}应该用你使用的实际值代替.查找UninstallStringQuietUninstallString值并使用该Exec函数从InitializeSetup()事件函数运行它.

更新:使用[Run]-section条目删除了非工作替代解决方案{uninstallexe}- 感谢所有指出这一点的评论者!

  • 一个警告:对于64位Windows上的32位应用程序,路径可能是`Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{AppId} \` (11认同)
  • 是否有可提供的示例代码? (5认同)
  • @Adrian:虽然在物理层可能也是如此,但我认为Inno使用的WinAPI调用已经处理好了 - 至少如果setup.exe本身是一个32位进程. (4认同)
  • 我认为`[Run]`部分解决方案不会起作用,因为它在安装过程中运行得太晚了.从Inno设置手册:*[运行]部分是可选的,并指定在程序成功安装后但在安装程序显示最终对话框之前要执行的任意数量的程序.* (3认同)

mla*_*aan 6

使用Inno Setup时,除非该版本由其他安装程序安装,否则没有理由卸载以前的版本.否则,将自动处理升级.

  • 不,它不会,您可以向脚本添加条目以在更新期间处理结构更改. (11认同)
  • 我们的程序结构发生了变化,因此需要卸载旧版本. (6认同)
  • 如果升级第三方库(例如DevExpress),该库在DLL名称中具有版本号(例如以前安装的15.1,现在安装的15.2),则您要删除旧版本。恕我直言,这是一个很好的理由。 (2认同)

rog*_*ack 6

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

; attempt to remove previous versions' icons
[InstallDelete]
Type: filesandordirs; Name: {group}\*;
Run Code Online (Sandbox Code Playgroud)

这是在"安装开始时"运行,所以基本上删除旧图标,完成后仍然会在那里安装新图标.

我只是在每次安装时执行此操作"万一有任何更改"图标明智(无论如何都会重新安装).