Reading a whole section from an inifile

How to read a whole section from an INI file.

[Script]

1000=DoSomething,1,2

1000=DoSomethingElse,3,4



When using TIniFile.ReadSectionValues only one of these items will be returned. Here is a procedure which will read the whole section.



procedure ReadWholeIniSection(FileName: string; SectionName: string; Strings: TStrings);

var

  P: PChar;

  Buffer: PChar;

  BufferSize: Integer;

begin

  Strings.BeginUpdate;

  BufferSize := 10 * 1024 * 1024;

  GetMem(Buffer, BufferSize);

  try

    BufferSize := GetPrivateProfileSection(PChar(SectionName), Buffer, BufferSize, PChar(FileName));

    if BufferSize = 0 then

      Exit;

    P := Buffer;

    while P^ <> #0 do



    begin

      Strings.Add(P);

      Inc(P, StrLen(P) + 1);

    end;

  finally

    Strings.EndUpdate;

    if Assigned(Buffer) then

      FreeMem(Buffer);

  end;

end;

 

Share this article!

Follow us!

Find more helpful articles: