Re: ini files

Posted by webmaster Guido on May 03, 2001 at 02:45:58:

In Reply to: ini files posted by Antonio Ocava on May 02, 2001 at 01:07:48:

: do you guys have examples or source on how to store or restore from an ini file for the settings on my application? thanks in advance.
-----

To start with, add "INIFILES" to the USES-clause of the unit. Then, look up "TIniFile" in Delphi's Help ;-)
Just two quick examples:

1. Writing to an INI-file:

procedure TformMain.btnWriteIniClick(Sender: TObject);
const
  S = 'Test';
  N = 10;
  B = True;
var
  Inifile: TInifile;
begin
  Inifile := TInifile.Create('C:\Test\Test.ini');
  with IniFile do begin
    WriteString('Section01', 'Key01', S);
    WriteInteger('Section01', 'Key02', N);
    WriteBool('Section01', 'Key03', B);
    Free;
  end;
end;

Attempting to write to a non-existent section or to a non-existent key does not generate an error. Instead, WriteString, WriteInteger, and so on... create the section and key and set its initial value to what you specify as the third parameter; also the INI-file is created in these cases.

2. Reading from an INI-file:

procedure TformMain.btnReadIniClick(Sender: TObject);
var
  S: string;
  N: integer;
  B: Boolean;
  Inifile: TInifile;
begin
  Inifile := TInifile.Create('C:\Test\Test.ini');
  with IniFile do begin
    S := ReadString('Section01', 'Key01', 'Nothing found');
    N := ReadInteger('Section01', 'Key02', 0);
    B := ReadBool('Section01', 'Key03', False);
    Free;
  end;
end;

Note: attempting to read from a non-existent section or from a non-existent key does not generate an error. Instead, ReadString, ReadInteger, and so on... return the default value that you specify as the third parameter (in the example above: 'Nothing Found' or 0 or False).