Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 3 - Working with INI Files in Delphi
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Working with INI Files in Delphi
By: Danish Ahmed
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 6
    2007-09-04

    Table of Contents:
  • Working with INI Files in Delphi
  • TIniFile and TMemIniFile Classes
  • Working with Sections
  • TRegistryIniFile Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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;

    More Delphi-Kylix Articles
    More By Danish Ahmed


     

    DELPHI-KYLIX ARTICLES

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory


    Iron Speed





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway