运行 部分命令中的 Inno 设置错误处理(忽略)

Inno setup error handeling (ignore) in RUN Section commands

我有一个.net dll,可以通过RegAsm.net 3.5.net 4.5

注册

我在我的设置脚本中使用了这些代码:

[Run]
Filename: "{dotnet40}\RegAsm.exe"; Parameters: "my.dll"; WorkingDir: "{app}"; Flags: skipifdoesntexist; StatusMsg: "Registering Controls."
Filename: "{dotnet4064}\RegAsm.exe"; Parameters: "my.dll"; WorkingDir: "{app}"; Flags: skipifdoesntexist; StatusMsg: "Registering Controls."
Filename: "{dotnet20}\RegAsm.exe"; Parameters: "my.dll"; WorkingDir: "{app}"; Flags: skipifdoesntexist; StatusMsg: "Registering Controls."
Filename: "{dotnet2064}\RegAsm.exe"; Parameters: "my.dll"; WorkingDir: "{app}"; Flags: skipifdoesntexist; StatusMsg: "Registering Controls."

但是

我们在某些情况下会遇到这样的错误:如果目标机器上没有 .net 3.5

我猜错误原因是:

{dotnet20}

.NET Framework version 2.0 root directory. {dotnet20} is equivalent to {dotnet2032} unless the install is running in 64-bit mode, in which case it is equivalent to {dotnet2064}.

An exception will be raised if an attempt is made to expand this constant on a system with no .NET Framework version 2.0 present.

我的问题是如何处理和忽略此异常并防止设置回滚:

Internal error: .Net Framework version 2.0 not found.

如果您想坚持 [Run] 部分而不是将其写在脚本代码中,那么我认为您没有太多选择。每当无法扩展常量时都会引发异常,这就是这种情况。我能想到的唯一选择是添加一个 Check 函数,该函数将尝试在 protected try..except 块中扩展常量并防止在以下情况下处理条目引发异常。类似如下(根据您的代码,缩短):

[Run]
Filename: "{dotnet20}\RegAsm.exe"; Parameters: "File.dll"; Check: RegAsmDotNet20Exists

[Code]
function RegAsmDotNet20Exists: Boolean;
begin
  try
    // process the entry only if the target binary could be found (and so the
    // folder constant could have been properly expanded)
    Result := FileExists(ExpandConstant('{dotnet20}\RegAsm.exe'));
  except
    // this is the fallback in case the folder constant could not be expanded,
    // or something unexpected happened when checking if the binary file to be
    // executed exists; in such case, don't process the entry
    Result := False;
  end;
end;

另一个更简洁、更安全的选择是完全从某些安装后事件的 [Code] 部分进行程序集注册。即使您在使用这些常量时仍然需要捕获异常,您也可以更好地控制该工具(例如,如果该工具使用它,您可以获取退出代码以获得错误原因)。