在 Inno Setup 中取消安装

Canceling an installation in Inno Setup

我正在用 C# 开发应用程序。我正在尝试使用 Inno Setup 创建安装包,但我需要检查用户是否安装了 .NET 框架。我这样做了,但问题来了:如果用户不想安装 .NET 4,程序需要取消安装。我该怎么做?

[Run]
Filename: "{app}\dotNetFx40_Full_x86_x64.exe"; Check: FrameworkIsNotInstalled
Filename: "{app}\sis_visu_ipccV2.0.exe"; Description: "{cm:LaunchProgram,SisIPCCAR4}"; Flags: nowait postinstall skipifsilent

[Code]
function FrameworkIsNotInstalled: Boolean;
begin
  if MsgBox('Foi detectado que seu computador não possui o .NET Framework 4.0. Para que o aplicativo execute normalmente é necessário tê-lo instalado. Deseja instalar? ', mbConfirmation, MB_YESNO or MB_DEFBUTTON2) = IDYES then
   begin
     Result := not RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\.NETFramework\policy\v4.0');
    end
    else begin
      Abort;
    end; 
end;

一开始我会检查并请求.NET安装的许可。

如果用户选择NOT安装.NET,安装过程将终止。

如果用户AGREE安装.NET,安装过程会运行正常,.NET会在安装结束时安装(通过RUN部分) .


您可以将其从 RUN 部分移至 BeforeInstall 或选择其他解决方案,但这需要编写额外的代码。


示例:

[Run]
Filename: "{app}\dotNetFx40_Full_x86_x64.exe"; WorkingDir: "{app}"; 
 Parameters: "/passive /norestart"; Flags: waituntilterminated skipifdoesntexist; 
 StatusMsg: "{cm:dotNetInstallation}"; Check: not dotNetInstalled
Filename: "{app}\sis_visu_ipccV2.0.exe"; Description: "{cm:LaunchProgram,SisIPCCAR4}"; 
 Flags: nowait postinstall skipifsilent

[CustomMessages]
dotNETnotpresent=Foi detectado que seu computador não possui o .NET Framework 4.0. Para que o aplicativo execute normalmente é necessário tê-lo instalado. %n%nDeseja instalar? 
dotNetInstallation=Installation of .NET Framework 4.0 in progress...

[Code]
var
    dotNetBool: Boolean;

function InitializeSetup(): Boolean;
var
  Q: Integer;
begin
    Result := False;
    dotNetBool := False;
    if not RegKeyExists(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\.NETFramework\policy\v4.0') then begin
    //Registry entry was not found, quesion will appear
        Q := MsgBox(ExpandConstant('{cm:dotNETnotpresent}'), mbInformation, MB_YESNO);      
        if Q = IDYES then begin
        //If the asnwer is YES, Setup will initialize 
        //If the answer is NO, Setup will terminate
            Result := True;
        end;
    end
    else begin
    //Registry entry was found, Setup will initialize
        dotNetBool := True;
        Result := True;
    end;
end;

function dotNetInstalled: Boolean;
begin
  Result := dotNetBool;
end;