有没有一种方法可以通过 TMemIniFile 或 TIniFile 在 Delphi 中更改 INI 文件的节名称?
我试图寻找,但找不到。
没有“重命名部分”方法,但此功能可以为您解决问题。
function RenameSection(IniFile:TCustomIniFile; FromName,ToName:string):boolean; //Success returns True.
var List: TStrings;
s,n,v: string;
i: integer;
begin
if IniFile.SectionExists(ToName) then Exit(False);
if not IniFile.SectionExists(FromName) then Exit(False);
List:=TStringList.Create;
IniFile.ReadSectionValues(FromName,List); //Actually reads names AND values.
for s in List do begin
i:=Pos('=',s);
n:=Copy(s,1,i-1);
v:=copy(s,i+1,length(s));
IniFile.WriteString(ToName,n,v);
end;
List.free;
IniFile.EraseSection(FromName);
IniFile.UpdateFile;
Result:=True;
end;