Home arrow Delphi-Kylix arrow Page 3 - Working with INI Files in Delphi
DELPHI-KYLIX

Working with INI Files in Delphi


Microsoft has taken steps to phase out .ini or initialization files, but they are too useful in too many situations to completely disappear just yet. For example, there are specific cases where you would want to use an INI file when developing a cross-platform application. This article explains how to create and use an INI file in Delphi.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 25
September 04, 2007
TABLE OF CONTENTS:
  1. · Working with INI Files in Delphi
  2. · TIniFile and TMemIniFile Classes
  3. · Working with Sections
  4. · TRegistryIniFile Class

print this article
SEARCH DEVARTICLES

Working with INI Files in Delphi - Working with Sections
(Page 3 of 4 )

The TIniFile class also provides methods to read and erase entire sections of the INI file at one go. The ReadSections method can be used to read all sections of an INI file to a string list, but if you want to read all the keys of the section you need to call the ReadSection; routine and pass the Section name as the first parameter. To read all the keys and their values in a specific section you can use the method named ReadSectionValues. This method, like the ReadSection method, requires you to pass the name of the section as the first parameter and specify a TStringList object as the second parameter. Below are examples of all four methods. I have added four buttons to the form and named them btnReadSections, btnReadSection, btnReadSectionValues, btnEraseSection. I am including the corresponding routines within the event handlers of the buttons.

procedure Tform1.btnReadSections(Sender:TObject);
var
  myIniFile:TIniFile;
begin
  myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
  try
    myIniFile.ReadSections(Memo1.Lines);
  finally
    myIniFile.Free;
  end;
end;

procedure Tform1.btnReadSection(Sender:TObject);
var
  myIniFile:TIniFile;
begin
  myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
  try
    myIniFile.ReadSection('CompanyInfo',Memo1.Lines);
  finally
    myIniFile.Free;
  end;
end;
procedure Tform1.btnReadSectionValues(Sender:TObject);
var
  myIniFile:TIniFile;
begin
  myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
  try
    myIniFile.ReadSectionValues('CompanyInfo',Memo1.Lines);
  finally
    myIniFile.Free;
  end;
end;

To delete an entire section call the EraseSection method.

procedure Tform1.btnEraseSection(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.EraseSection('CompanyInfo');
finally
myIniFile.Free;
end;
end;

Now that we have examined most of the Read and Write methods of the TIniFile class, let us implement the class in a real world application. The Delphi 7 help file provides a code snippet that stores information pertaining to the position of the window in relation to the desktop in the INI file and uses it to display the window in the same position when the application is started again. I am providing a modified version of the code that reads from and writes desktop coordinates to the INI file. It is obvious that, since we intend to display the window in the same position as it was last time, we should read the values from the INI file on the form's OnCreate event and write the values to the INI file in the form's OnClose event handler.

procedure TForm1.FormCreate(Sender: TObject);
var
myIniFile: TIniFile;
begin
myIniFile := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
try
Top := myIniFile .ReadInteger( 'Form', 'Top', 100 );
Left := myIniFile.ReadInteger( 'Form', 'Left', 100 );
Caption := myIniFile.ReadString( 'Form', 'Caption', 'Form1' );
if myIniFile.ReadBool( 'Form', 'InitMax', false ) then
WindowState = wsMaximized
else
WindowState = wsNormal;
finally
myIniFile.Free;
end;
end;

Note:The code after the TINIFile's Constructor call is put in guarded try/finally blocks to prevent memory leaks.

In the above code we are instantiating a TIniFile object, assigning the applications INI file to it and changing the Top, Left, Caption and WindowState properties of Form1 with the values retrieved from the INI file. The third parameter of the two ReadInteger methods contain values of 100 so that if the INI file does not contain a value for these properties, these values would be substituted and Form1's Top and Left properties should have values of 100 each.

Now we come to the writing part. Since we intend to save the position of the form when the application terminates, the OnClose event handler of Form1 would be the perfect place to include the code that writes values to the INI file.

procedure TForm1.FormClose(Sender: TObject; var Action TCloseAction)
var
myIniFile: TIniFile;
begin
myIniFile := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
try
myIniFile.WriteInteger( 'Form', 'Top', Top);
myIniFile.WriteInteger( 'Form', 'Left', Left);
myIniFile.WriteString( 'Form', 'Caption', Caption );
myIniFile.WriteBool( 'Form', 'InitMax', WindowState = wsMaximized ); 
finally
myIniFile.Free;
end;
end;


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials