How to change the section name of an INI file via TMemIniFile or TIniFile?

257 Views Asked by At

Is there a method to change the section name of an INI file in Delphi, via TMemIniFile or TIniFile?

I try to find but cannot find one.

1

There are 1 best solutions below

10
Philip J. Rayment On

There is no 'rename section' method, but this function will do the trick for you.

function RenameSection(IniFile:TCustomIniFile; FromName,ToName:string):boolean; //Success returns True.
var List:   TStrings;
    n,v:    string;
    i:      integer;
begin
  if IniFile.SectionExists(ToName) then Exit(False);
  if not IniFile.SectionExists(FromName) then Exit(False);

  List:=TStringList.Create;
  try
    IniFile.ReadSectionValues(FromName,List);  //Actually reads names AND values.
    for i:=0 to List.count-1 do begin
      n:=List.Names[i];
      v:=List.ValueFromIndex[i];
      IniFile.WriteString(ToName,n,v);
    end;
  finally
    List.free;
  end;
  IniFile.EraseSection(FromName);
  IniFile.UpdateFile;
  Result:=True;
end;