2

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

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

  • How do you write subsections?
  • How do subsections actually appear in the INI file?
  • And, once you've called ReadSubSections, how do you use the result to read data from a particular subsection?
7
  • I think it only is implemented in TMemIniFile docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/… so you just can try to use it, and then read the ini file in notepad to check the formatting. But since it is TMemIniFile-only and non-standard, i'd refrain from using it. If you really need to use a storage structured in more than two layers - just use JSON SuperObject Jan 10, 2014 at 13:41
  • @Arioch'The I don't see anything abut writing subsections in that page? Did I miss something...
    – Roddy
    Jan 10, 2014 at 13:46
  • 1
    well, not only. It also is implemented in registry. and that probably is where it came from. docwiki.embarcadero.com/Libraries/XE2/en/… so you ccan try to read from TRegistryIniFile to a temporary stringlist, then from the SL into TMEmIniFile, then save it to the ini-file and read it in notepad. I think the intention is to use SubSections name just instead of Sections names in Read*** and Write*** methods. And i blindly guess they would be written with back-slashes just like the regedit's ini-files with *.reg extension. But you do try Jan 10, 2014 at 13:46
  • Again, rather than using non-standard non-documented extension to ini files i think it would make more sense to use standard JSON via some well-credited library (dbExpress is not) Jan 10, 2014 at 13:48
  • 1
    For both INI and JSON there is the only reliable method of persisting comments to include them, as some keys with a special convention about their names. Or to only use read-only access. Both Windows (TIniFile) and Delphi (TMemInifile) implementations give no warranties about non-data and may overwrite file if they wish (and TMemIniFile would really do). So if you need comments either stick with XML, or meake a dedicated comment values Jan 10, 2014 at 13:54

1 Answer 1

4

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
9
  • 2
    The default section name separator is a backslash.
    – Uwe Raabe
    Jan 10, 2014 at 13:52
  • @UweRaabe Aye! i guessed that! TMemIniFile tries to mimic RegEdit.exe .reg files with cooperation with TRegIniFile :-D Jan 10, 2014 at 13:55
  • @UweRaabe. I'd seen that - but the code posted works correctly. Very weird.
    – Roddy
    Jan 10, 2014 at 13:56
  • 1
    Actually it's worse than that, at least in D2010. If you use ReadSubSections('MAIN', slValues) in the above example you get 'ECTION.SUBSECTION_A' and 'ECTION.SUBSECTION_B', so the separator is basically ignored!!!
    – Roddy
    Jan 10, 2014 at 14:06
  • 1
    @Roddy, yeah - this feature is simply not fully implemented. I wouldn't suggest to use it anyway.
    – Uwe Raabe
    Jan 10, 2014 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.