Roddy
Roddy

Reputation: 68054

SubSection support in .INI files

Delphi's RTL has a tantalizing function: TCustomIniFile.ReadSubSections

But this doesn't really seem to be properly explained anywhere.

Upvotes: 2

Views: 4656

Answers (1)

Agustin Seifert
Agustin Seifert

Reputation: 1968

To write subsections just join your section name and subsection name with a backslash.

[section_name\subsection_name]

procedure Foo;
var
  LIniFile: TIniFile;
  slValues: TStringList;
  IniFileName: string;
begin
  IniFileName := ''; //your ini file
  LIniFile := TIniFile.Create(IniFileName);
  try
    //SECTION = MAINSECTION
    //SUBSECTIONS = SUBSECTION_A
    //            = SUBSECTION_B
    LIniFile.WriteString('MAINSECTION\SUBSECTION_A','IDENT','A');
    LIniFile.WriteString('MAINSECTION\SUBSECTION_B','IDENT','B');

    slValues := TStringList.Create;
    try
      LIniFile.ReadSubSections('MAINSECTION', slValues);
      //slValues.count = 2
      //slValues.strings[0] = SUBSECTION_A
      //slValues.strings[1] = SUBSECTION_B
      // do your stuff here!
    finally
      slValues.Free;
    end;
  finally
    LIniFile.Free;
  end;
end;

the ini file looks like:

[MAINSECTION\SUBSECTION_A]
IDENT=A
[MAINSECTION\SUBSECTION_B]
IDENT=B

Upvotes: 4

Related Questions