本文介绍了使用Inno Setup替换文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Inno Setup(基于Delphi)替换文本文件中的文本时遇到问题.

Hi I have a problem with replacing a text in a textfile with Inno Setup (Delphi based).

我的代码:

procedure  FileReplaceString(const  FileName,  searchstring,  replacestring:  string);
var
    fs:  TFileStream;
    S:  string;
begin
    fs  :=  TFileStream.Create(FileName,  fmOpenread  or  fmShareDenyNone);
    try
        SetLength(S,  fs.Size);
        fs.ReadBuffer(S[1],  fs.Size);
    finally
        fs.Free;
    end;
    { the compiler stops here with: unknown identifier 'StringReplace' }
    S := StringReplace(S,  SearchString,  replaceString,  [rfReplaceAll,  rfIgnoreCase]);
    fs  :=  TFileStream.Create(FileName,  fmCreate);
    try
        fs.WriteBuffer(S[1],  Length(S));
    finally
        fs.Free;
    end;
end;

我发现我必须使用StringChange(),但是我不知道如何在我的代码中使用它.我对Delphi或Inno Setup不太了解.我希望你能帮助我.

I found out that I have to use StringChange(), instead but I don't know how to use it with my code. I don't know too much about Delphi or Inno Setup.I hope you can help me.

推荐答案

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

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.

这篇关于使用Inno Setup替换文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:38