且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Inno Setup替换文件中的文本

更新时间:2023-02-23 14:40:28

我希望此函数可以完成此工作:

I hope this function does the job:

function FileReplaceString(const FileName, SearchString, ReplaceString: string):boolean;
var
  MyFile : TStrings;
  MyText : string;
begin
  MyFile := TStringList.Create;

  try
    result := true;

    try
      MyFile.LoadFromFile(FileName);
      MyText := MyFile.Text;

      { Only save if text has been changed. }
      if StringChangeEx(MyText, SearchString, ReplaceString, True) > 0 then
      begin;
        MyFile.Text := MyText;
        MyFile.SaveToFile(FileName);
      end;
    except
      result := false;
    end;
  finally
    MyFile.Free;
  end;
end;

向TLama表示感谢.

Kudos to TLama for feedback.