由Inno Setup安装的安装程序安装多个软件副本

时间:2018-05-30 16:51:53

标签: inno-setup

我们使用Inno Setup准备安装程序。因此用户安装该软件。发布新版本时,新安装程序会更新软件。到目前为止一切都很好。

但有些人希望同时拥有旧版本的软件和新版本。

是否可以让安装人员询问用户是想要更新当前安装还是并排安装新版本。

1 个答案:

答案 0 :(得分:1)

InitializeSetup event function中,检测是否已安装该应用程序。如果是,请询问用户,如果他/她选择安装新副本,请将AppIdDefaultDirName更改为特定于版本的值以强制进行新安装。

[Setup]
#define AppId "My Program"
#define SetupReg "Software\Microsoft\Windows\CurrentVersion\Uninstall\" + AppId + "_is1"
#define DisplayVersionReg "DisplayVersion"
#define ApplicationVersion() \
   ParseVersion('MyProg.exe', Local[0], Local[1], Local[2], Local[3]), \
   Str(Local[0]) + "." + Str(Local[1])

[Setup]
AppId={code:GetAppId}
AppName=My Program
AppVersion={#ApplicationVersion}
DefaultDirName={code:GetDefaultDirName}
UsePreviousLanguage=no

[Files]
Source: "MyProg.exe"; DestDir: "{app}"
[Code]

var
  AppId: string;
  DefaultDirName: string;

function GetAppId(Param: string): string;
begin
  Result := AppId;
  Log('AppId = ' + Result);
end;

function GetDefaultDirName(Param: string): string;
begin
  Result := DefaultDirName;
  Log('DefaultDirName = ' + Result);
end;

function InitializeSetup(): Boolean;
var
  PrevVersion: string;
  CurVersion: string;
  Message: string;
  R: Integer;
begin
  CurVersion := '{#ApplicationVersion}';
  Log(Format('Installing "%s"', [CurVersion]));
  Result := True;

  AppId := '{#AppId}';
  DefaultDirName := ExpandConstant('{pf}\My Program');

  if RegQueryStringValue(HKLM, '{#SetupReg}', '{#DisplayVersionReg}', PrevVersion) or
     RegQueryStringValue(HKCU, '{#SetupReg}', '{#DisplayVersionReg}', PrevVersion) then
  begin
    Message :=
      Format(
        'Version is %s already installed. Do you want to upgrade to %s?'#13#10#13#10+
        'Press Yes, to replace %0:s with %1:s.'#13#10+
        'Press No, to keep %0:s and add separate installation of %1:s.'#13#10, [
        PrevVersion, CurVersion]);
    R := MsgBox(Message, mbConfirmation, MB_YESNOCANCEL);
    if R = IDYES then
    begin
      Log('Use chose to replace previous installation');
    end
      else
    if R = IDNO then
    begin
      AppId := AppId + CurVersion;
      DefaultDirName := DefaultDirName + ' ' + CurVersion;
      Log('Use chose to install new copy - using ID ' + AppId);
    end
      else
    begin
      Log('Use chose to cancel installation');
      Result := False;
    end;
  end;
end;

enter image description here