Home arrow Delphi-Kylix arrow Page 2 - Working with INI Files in Delphi

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
  1. · Working with INI Files in Delphi
  2. · TIniFile and TMemIniFile Classes
  3. · Working with Sections
  4. · TRegistryIniFile Class

print this article

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.



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:


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

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);
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
myIniFile.WriteInteger('CompanyInfo',' IntVal',100);

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:


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);

  myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
    sVal:= myIniFile.ReadString('CompanyInfo,'CompanyName','No Value');
    ShowMessage(sVal + IntToStr(iVal));

blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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