您好我用Inno Setup(基于Delphi)替换文本文件中的文本有问题.
我的代码:
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.
我希望你能帮助我.
解决方法
我希望这个功能完成这项工作:
function FileReplaceString(const FileName,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,ReplaceString,True) > 0 then begin; MyFile.Text := MyText; MyFile.SaveToFile(FileName); end; except result := false; end; finally MyFile.Free; end; end;
感谢TLama提供反馈意见.