TStringList in TIniFile

 

unit IniStr;

{Written by Ed Jordan}

interface

 

uses Classes;

 

type

TIniStringlist = class( TStringList )

public

  procedure LoadFromIni( const FileName, Section: string);

  procedure SaveToIni( const FileName, Section: string);

end;

 

implementation

 

uses IniFiles, SysUtils;

 

procedure TIniStringList.LoadFromIni( const FileName,Section: string);

var

  Index: Integer;

  Line: string;

begin

  with TIniFile.Create( FileName ) do

  try

  ReadSectionValues( Section, Self);

  for Index:= 0 to Count - 1 do

  begin

  { Remove the identifier name ...}

  Line:= Values[ IntToStr( Index ) ];

  { Delete the tilde ... }

  System.Delete( Line, 1, 1);

  Strings[ Index ]:= Line;

  end;

  finally

  Free;

  end;

end;

 

procedure TIniStringList.SaveToIni( const FileName, Section: string);

var

  Index: Integer;

  Line: string;

begin

  with TIniFile.Create( FileName ) do

  try

  EraseSection( Section );

  for Index:= 0 to Count - 1 do

  begin

  { Preserve leading white space, blank lines ...}

  Line:= '~' + Strings[ Index ];

  WriteString( Section, IntToStr( Index ), Line);

  end;

  finally

  Free;

  end;

end;

 

end.

 

 

Usage:

 

var

  L: TIniStringList;

begin

  L:= TIniStringList.Create;

  L.LoadFromIni('MyFile.Ini', 'Alati');

  {process L..}

  L.Free;

end.