.net - 在 Inno Setup Run 部分命令中扩展 .NET 路径常量时的错误处理/忽略

标签 .net inno-setup

我有一个 .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 和 .NET 4.5,则效果很好
  • 我的脚本中有一个函数可以在 InitializeSetup 中检查 .net。所以我知道系统上安装了以下版本之一:v3.5 v4 v4.5

但是

在某些情况下我们会遇到这样的错误:如果目标机器上没有 .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 block 中的常量并防止引发异常时要处理的条目。类似如下(根据您的代码,缩短):

[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] 部分进行程序集注册。即使您在使用这些常量时仍然需要捕获异常,您也可以更好地控制该工具(例如,如果该工具使用它,您可以获取退出代码以获得错误原因)。

关于.net - 在 Inno Setup Run 部分命令中扩展 .NET 路径常量时的错误处理/忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28928772/

相关文章:

inno-setup - 如何使用 Inno Setup 安装 SQL Server 2008 Express?

inno-setup - 在 Inno Setup 中取消选择在 "Portable mode"中创建开始菜单和桌面图标

c# - 从列表值创建对

.net - Entity Framework 代码优先迁移错误

.net - 从包含字母和空格的字符串中解析整数 - C#

Java 泛型类类型的 C# 等效语法

wix - 使用其他子 MSI 作为先决条件创建 MSI 的最佳做法是什么?

inno-setup - 在过程调用中使用常量和代码(inno setup)

windows - 我怎么能说 64 位,使用 c :\program files (x86) and for 32-bit use the c:\program files?

c# - 查找每个月的第三个星期日出现在给定的两个日期之间