Inno Setup - 在 [代码] 中定义默认路径

Inno Setup - Defining a default path inside [code]

我正在尝试读取 Windows 注册表,以便我的应用程序更新可以检索以前保存的安装路径作为其 DefaultDirName。

我在某处读到我应该调用函数,例如:

DefaultDirName="{code:GetPath}"

问题是我需要定义一个默认路径,以防函数找不到以前的路径。例如,'C:\MyPath'。所以我这样做了:

[Code]
function GetPath(Value: String): String;
var
  OrigPath: string;
begin
  Result := '{sd}\MyPath';
  if RegQueryStringValue(HKCU, 'Software\MyApp', 'PathExec', OrigPath) then
     Result := OrigPath;
end;

那是行不通的。当我 运行 设置时,在目标目录对话框中我得到的字面意思是 "C:\PathOfMySetup\{sd}\MyPath",而不是 "C:\MyPath"。

为了在系统驱动器上创建 "MyPath",我应该在第一个 "Result := " 行写什么?

谢谢。

Pascal 脚本中的常量没有神奇地扩展。您必须使用 ExpandConstant function:

显式扩展它们
Result := ExpandConstant('{sd}\MyPath');