Skip Navigation Links.
Reading and writing to .ini files.
Language(s):Delphi
Category(s):

Using the profile string APIs to read and write to .ini files (Note: This example only works on the 16 bit platform!!!)

{
  This sample code is presented as is.
  Although every reasonable effort has been
  made to insure the soundness of the example
  below, Idioma Software inc. makes no warranty
  of any kind with regard to this program sample
  either implicitly or explicitly.

  This program example may be freely distributed for the
  use of writing computer programs only. Any other use of
  this material requires written permission from Idioma Software inc.

  (c) 1997 Idioma Software inc. All rights reserved.
 }

{
  This example shows how to use .ini files.
}
Procedure IniIO;
{Author Jon Vote 10/97}

const FILE_NAME = 'test.ini';
      CRLF = chr(13) + chr(10);

var IniFile : TIniFile;

begin
  {Create the .ini file}
  IniFile := TIniFile.Create(FILE_NAME);

  {Write some strings}
  IniFile.WriteString('Strings', 'One', 'This is a string');
  IniFile.WriteString('Strings', 'Two', 'This is another string');

  {Write some integers}
  IniFile.WriteInteger('Numbers', 'Number1', 5);
  IniFile.WriteInteger('Numbers', 'Number2', 5);

  {Write some booleans}
  IniFile.WriteBool('Bools', 'Value1', True);
  IniFile.WriteBool('Bools', 'Value2', False);

  ShowMessage(FILE_NAME + ' in Windows\System has been created
  with test values');

  {Retrieve some of the values - the third parameter is the default}
  ShowMessage ('The second string entry is:' +  CRLF + CRLF +
  '[Strings]' + CRLF +
                    + 'Two=' + IniFile.ReadString('Strings', 'Two', ' '));
  ShowMessage ('The first numeric entry is:' + CRLF + CRLF +
  '[Numbers]' + CRLF +
                    + 'Number1=' + IntToStr(IniFile.ReadInteger('Numbers',
'Number1', -1)));

  if IniFile.ReadBool('Bools', 'Value1', false) then
     ShowMessage('The first boolean value is True')
  else
     ShowMessage('The first boolean value is False');

  if IniFile.ReadBool('Bools', 'Value2', false) then
     ShowMessage('The second boolean value is True')
  else
     ShowMessage('The second boolean value is False');

end;


This article has been viewed 3331 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.