用用户输入的输入替换安装的文本文件中的占位符 [英] Replace placeholder in an installed text file with input entered by user

查看:75
本文介绍了用用户输入的输入替换安装的文本文件中的占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As part of an Inno Setup built installer, I want to output the text field that the user enters into the installer to a text file.

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

So far I have the below:

[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;

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

However, when I run the installer and enter the field it does not output to the text file.

如果我将PrimaryServerPage.Values[0]替换为数字,则会成功将其输出到文本文件.

If I replace PrimaryServerPage.Values[0] with a number this is successfully output to the text file.

任何人都可以针对我可能要去哪里的问题提供帮助或建议吗?

Can anyone help or offer suggestions on where I might be going wrong?

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

Also, following this I actually want to output this value into the middle of an existing text file, is this possible? For example here is the config file I wish to insert it into. Value to be added into "ENTER VALUE HERE!" Can this be added as a last step of the installation? The config file will not exist until after the install is complete?

###############################################################################
#
#    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也会把我赶到那里.

Work in progress, getting stuck with getting the text file output working (I think I may be misunderstanding the post on this) before I look at the replace although any guidance around that would be great as I'm sure my inexperience with Inno is going to catch me out there too.

[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;

推荐答案

结合以下两个问题的答案:

Combining answers to these two questions:

  • Replace a text in a file with Inno Setup (FileReplaceString function)
  • Inno Setup Compiler: How to modify file content (use of CurStepChanged(ssPostInstall) event function)

您将获得如下代码:

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;


要在安装过程的早期进行替换,请参见:


To do the replacement earlier in the installation process, see also:

  • Inno Setup: How to run a code procedure in Run section or before Run section?
  • How to execute cmd commands in Inno Setup

这篇关于用用户输入的输入替换安装的文本文件中的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆