1

我正在尝试将我的代码从 Inno Setup 5 转换为 6。我无法使以下代码工作。

我的旧工作代码是:

procedure CurStepChanged(CurStep: TSetupStep);
var
  FileData: String;
begin
  if (CurStep = ssInstall) then
  begin
    LoadStringFromFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData);
    StringChange(FileData, 'ScvProdPath', ExpandConstant('{code:GetSvcDir|0}\'));
    StringChange(FileData, 'ProdSitePath', ExpandConstant('{code:GetWebDir|0}\'));
    StringChange(FileData, 'ProdAuthPath', ExpandConstant('{code:GetWebDir|1}\'));
    StringChange(FileData, '444', ExpandConstant('{code:GetConfig|4}'));
    StringChange(FileData, '8732', ExpandConstant('{code:GetConfig|3}'));
    StringChange(FileData, 'RV_ExceptionsPath', ExpandConstant('{code:GetSvcDir|2}\RVExceptions'));
    SaveStringToFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData, False);
  end;
end;

在阅读了一些这样的文章后,我试图改变它,但我无法让它工作。

procedure CurStepChanged(CurStep: TSetupStep);
var
  FileData: AnsiString;
  UnicodeStr: string;
begin
  if (CurStep = ssInstall) then
  begin
    LoadStringFromFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData);
    StringChangeEx(UnicodeStr, 'ScvProdPath', ExpandConstant('{code:GetSvcDir|0}\'), True);
    StringChangeEx(UnicodeStr, 'ProdSitePath', ExpandConstant('{code:GetWebDir|0}\'), True);
    StringChangeEx(UnicodeStr, 'ProdAuthPath', ExpandConstant('{code:GetWebDir|1}\'), True);
    StringChangeEx(UnicodeStr, '444', ExpandConstant('{code:GetConfig|4}'), True);
    StringChangeEx(UnicodeStr, '8732', ExpandConstant('{code:GetConfig|3}'), True);
    StringChangeEx(UnicodeStr, 'RV_ExceptionsPath', ExpandConstant('{code:GetSvcDir|2}\RVExceptions'), True);
    SaveStringToFile(ExpandConstant('{code:GetSvcDir|2}\PostSteps.ps1'), FileData, False);
  end;
end;

文件里面的文字是英文的,没什么特别的。请你帮助我好吗?

4

1 回答 1

1

正如我对您之前的问题所评论的那样,这与从 Inno Setup 5 升级到 Inno Setup 6 无关。Inno Setup 6 与 Inno Setup 5 代码兼容。您的问题是您使用了 Inno Setup 5 的旧 Ansi 版本。而 Inno Setup 6 只有 Unicode 版本。即使使用 Inno Setup 5,您也应该使用 Unicode 版本。请参阅从 Ansi 升级到 Inno Setup 的 Unicode 版本(任何缺点)

一般来说,您不应该使用AnsiString. 这是 Ansi 版本的遗产。使用AnsiString,如果用户在路径中使用非 ASCII 字符,您的代码将失败。

所以最后,你的问题或多或少是重复的:
Inno Setup replace a string in a UTF-8 file without BOM


无论如何,要回答您的字面问题:您永远不会分配UnicodeStr变量。

也与您之前的问题一样:不要使用ExpandConstant调用函数。

procedure CurStepChanged(CurStep: TSetupStep);
var
  FileData: AnsiString;
  UnicodeStr: string;
begin
  if (CurStep = ssInstall) then
  begin
    LoadStringFromFile(GetSvcDir('2') + '\PostSteps.ps1', FileData);
    UnicodeStr := FileData;
    StringChangeEx(UnicodeStr, 'ScvProdPath', GetSvcDir('0') + '\', True);
    StringChangeEx(UnicodeStr, 'ProdSitePath', GetWebDir('0') + '\', True);
    StringChangeEx(UnicodeStr, 'ProdAuthPath', GetWebDir('1') + '\', True);
    StringChangeEx(UnicodeStr, '444', GetConfig('4'), True);
    StringChangeEx(UnicodeStr, '8732', GetConfig('3'), True);
    StringChangeEx(UnicodeStr, 'RV_ExceptionsPath', GetSvcDir('2') + '\RVExceptions', True);
    FileData := UnicodeStr;
    SaveStringToFile(GetSvcDir('2') + '\PostSteps.ps1', FileData, False);
  end;
end;

(未经测试)

然后你可能甚至不需要这些函数——你可以将它们内联到上面的代码中,除非你在其他地方使用它们。

您还会发现AddBackslash功能很有用。

于 2019-06-22T09:10:08.507 回答