将已安装文本文件中的占位符替换为用户在Inno Setup中输入的输入



作为Inno Setup构建安装程序的一部分,我想将用户输入安装程序的文本字段输出到文本文件中。

到目前为止,我有以下内容:

[Code]
var
PrimaryServerPage: TInputQueryWizardPage;
PrimaryAddress: String;
procedure InitializeWizard;
begin
PrimaryServerPage := CreateInputQueryPage(wpWelcome,
'Primary Server Details', 'Where is you application installed?',
'Please specify the IP address or hostname of your Primary Server, ' +
'then click Next.');
PrimaryServerPage.Add('Primary Server IP/Hostname:', false); 
PrimaryAddress := PrimaryServerPage.Values[0];
SaveStringToFile('c:filename.txt', PrimaryAddress, True);
end;

但是,当我运行安装程序并输入字段时,它不会输出到文本文件。

如果我用数字替换PrimaryServerPage.Values[0],这将成功输出到文本文件。

任何人都可以帮助或提供关于我可能出错的建议吗?

另外,在此之后,我实际上想将此值输出到现有文本文件的中间,这可能吗? 例如,这里是我希望将其插入的配置文件。要添加到ENTER VALUE HERE!中的值 这可以添加为安装的最后一步吗?在安装完成之前配置文件将不存在?

###############################################################################
#
#    Configuration File.
#
###############################################################################
#
# This file is intended for advanced users. Please consult the documentation
# before modifying this file.
#
# NOTE: The hash (#) represents a comment.
#
#
# Define the name or IP address of the primary server.
# On secondary server installs, this value should be changed to point to the
# primary server.
#   Default: 127.0.0.1
#   Examples:  mainserver.localdomain.com, win2003, 1.2.3.4
#
# IMPORTANT: Please restart the Service" after
# changing this value.
#
ApplicationServer=ENTER VALUE HERE!

工作正在进行中,在我查看替换之前,陷入了让文本文件输出正常工作的困境(我想我可能误解了这方面的帖子(,尽管围绕它的任何指导都会很棒,因为我相信我对 Inno 的经验不足也会让我在那里。

[Code]
var
PrimaryServerPage: TInputQueryWizardPage;
PrimaryAddress: String;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
if(CurPageID = wpWelcome) then
begin
PrimaryServerPage := CreateInputQueryPage(wpWelcome,
'Application Server Details', 'Where is your app installed?',
'Please specify the IP address or hostname of your Application Server, ' +
'then click Next.');
PrimaryServerPage.Add('Primary Server IP/Hostname:', false); 
PrimaryAddress := PrimaryServerPage.Values[0];
SaveStringToFile('c:filename.txt', PrimaryAddress, True);
end;

Result :=True;
end;

结合这两个问题的答案:

  • 使用Inno Setup替换文件中的文本(FileReplaceString功能(
  • Inno 安装程序编译器:如何修改文件内容(使用CurStepChanged(ssPostInstall)事件功能(

你会得到一个代码,比如:

var
PrimaryServerPage: TInputQueryWizardPage;
function FileReplaceString(ReplaceString: string):boolean;
var
MyFile : TStrings;
MyText : string;
begin
Log('Replacing in file');
MyFile := TStringList.Create;
try
Result := true;
try
MyFile.LoadFromFile(ExpandConstant('{app}' + 'thefile.txt'));
Log('File loaded');
MyText := MyFile.Text;
{ Only save if text has been changed. }
if StringChangeEx(MyText, 'REPLACE_WITH_IP', ReplaceString, True) > 0 then
begin;
Log('IP address inserted');
MyFile.Text := MyText;
MyFile.SaveToFile(ExpandConstant('{app}' + 'thefile.txt'));
Log('File saved');
end;
except
Result := false;
end;
finally
MyFile.Free;
end;

Result := True;
end;
procedure InitializeWizard;
begin
PrimaryServerPage :=
CreateInputQueryPage(
wpWelcome, 'PaperCut Application Server Details', 'Where is PaperCut installed?',
'Please specify the IP address or hostname of your ' +
'Primary PaperCut Application Server, then click Next.');
PrimaryServerPage.Add('Primary Application Server IP/Hostname:', False);
end;   
procedure CurStepChanged(CurStep: TSetupStep);
begin
if CurStep = ssPostInstall then
begin
Log('File installed, replacing IP address');
FileReplaceString(PrimaryServerPage.Values[0]);
end;
end;

要在安装过程的早期进行更换,另请参阅:

  • Inno Setup:如何在"运行"部分或"运行"部分之前运行代码过程?
  • 如何在Inno Setup中执行cmd命令

最新更新