Delphi-Kylix
  Home arrow Delphi-Kylix arrow Page 2 - 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 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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
     
    Iron Speed
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Working with INI Files in Delphi - TIniFile and TMemIniFile Classes


    (Page 2 of 4 )

    Delphi VCL provides TIniFile and TMemIniFile classes to allow programmers to write and read data to an INI file from within their programs. The TMemIniFile class, unlike TIniFile classes, has no 64 KB size limit, but it does not write to the disk; it writes to a memory buffer and saves to disk only after the "updatefile" method has been called. To use the TIniFile class you need to include inifiles.pas unit file in the "uses" clause of your code.

    To instantiate a TIniFile class object you would be required to pass the name (which happens to be the only property of this class) of the INI file as the parameter of the constructor. If the file doesn't exist it will be created.

    var
     
    myIniFile:TIniFile;

    begin
     
    myIniFile:=TIniFile.Create('Test.ini');
    end;

    In the above instance the INI file is created and saved in the Windows folder since we have not specified a path, but it can be saved in any location as long as you provide a valid path along with the file name. However, it would be a better idea to save the file in the application folder and assign it the same name as the application itself. This is made easy by the ChangeFileExt routine which can be found in the SysUtils.pas unit file. This function takes the filename as the first parameter and changes its extension to the extension specified with the second parameter. Thus the above code can be now written as:

    var
     
    myIniFile:TIniFile;

    begin
     
    myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
    end;

    The INI file created will have the same filename as the executable and will be saved in the same location as well.

    Write and Read Methods

    The TIniFile class provides different "Write" and "Read" methods which you can use depending upon whether you are handling values or sections, and in case of the former, the data type you are handling. To write a string value to a .INI file you can call the WriteString method, but if the value is an integer you will need to call the WriteInteger method. In the same manner, you can call WriteDate, WriteBool, WriteDateTime, WriteTime, WriteFloat and WriteBinaryStream depending on the data type of the value.

     WriteString(const Section: string; const Ident: string; const Value: string);

    Each Write method requires three parameters. The first parameter identifies the section of the file. The second parameter,"Ident," identifies the key you want to write to and the third parameter contains the value that should be written with the key.

    uses inifiles.pas;
    ...
    procedure Tform1.btnWriteClick(Sender:TObject);
    var
     
    myIniFile:TIniFile;
    begin
     
    myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
     
    try
       
    myIniFile.WriteString('CompanyInfo','CompanyName','mindfire');
       
    myIniFile.WriteInteger('CompanyInfo',' IntVal',100);
     
    finally
       
    myIniFile.Free;
     
    end;
    end;

    The code above creates an INI file with the same name as the application. For example, if your project name is TestProject1, the TestProject1.ini file is created in the output folder. Open the INI file in notepad; you should be able to see the following:

    [CompanyInfo]
    CompanyName=mindfire
    IntVal=100

    Like Write methods, the TIniFile class also provides Read methods to read data from an INI file. The Read methods are identical to the Write methods in every way except that the data is read from the INI file and not written.

    ReadString(const Section:string; const Ident: string; const Value: string);

    The ReadString method also requires three parameters. The first parameter corresponds to the Section name, the second to the Key name and the third parameter contains the default value to be read in case the key does not contain any values. In the following example we will read from the INI file we created earlier and display the value in a message box. If the key does not contain any value, the text "No Value" will be displayed instead.

    procedure Tform1.btnReadClick(Sender:TObject);
    var
      myIniFile:TIniFile;
      sVal:string;
      iVal:integer;

    begin
      myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
      try
        sVal:= myIniFile.ReadString('CompanyInfo,'CompanyName','No Value');
        iVal:=myIniFile.ReadInteger('CompanyInfo,'IntVal',0);
        ShowMessage(sVal + IntToStr(iVal));
      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 1 hosted by Hostway