INI Files

Posted on 2004-08-10
Last Modified: 2010-04-05
What are INI files for and are they used in Delphi applications? Could you store info in these files and load them into your program, if so could someone give me an example on how to set that up?
Question by:c-prompt

Expert Comment

ID: 11763502
yes, they are used to store/retrieve information, mostly for configuration data. You can also use windows registry for this purpose.

For more information look at
LVL 17

Expert Comment

by:Wim ten Brink
ID: 11763638
The INI files format is a bit antiquated mechanism for storing a reasonable small amount of configuration data. It's biggest advantage is that it can be edited by using Notepad, thus you can make changes without starting the associated application. INI files are still pretty popular since they're easy to use and reasonable fast. Especially since INI files are divided in sections and every section has a key-value pair, they can have quite a few uses.

You could use the registry instead but here lies a minor problem. If your application is installed in a network environment, and needs default settings for all the PC's connected to it, storing this settings in the registry would just bring total chaos. Because every user will have his own set of registry settings, every user would need to know the default values and changing them globally is just impossible. Thus an INI file is useful for global configuration data while the registry is just a more personalized configuration set.

But at this moment, XML seems to become a new standard for storing configurations. Especially because XML is more like a tree with lots of levels while an INI file is just a list of key-values in a list of sections.

For the ise of Inifiles, see the IniFiles unit. The use is somewhat like this:

var IniFile: TInifile;
  IniFile := TIniFile.Create('YourINIFile.ini');
    AValue := IniFile.ReadString('Section','Key','DefaultValue');

That's writing and reading data... Integers in this case. See the helpfiles for more info.
LVL 12

Accepted Solution

Ivanov_G earned 90 total points
ID: 11763793
the INI file have format like this;



Basicly in IniFile you can store different type of Information. When you want to access it, you declare variable of type TIniFIle

  INI : TIniFile;
  lc_int : Integer;
  lc_str : String;
  // open the file
  Ini := TIniFile.Create('C:\Options.ini');
  // read integer
  lc_int := Ini.ReadInteger('GeneralOptions', 'Option1', 0);
  // read string
  lc_str := Ini.ReadString('GeneralOptions', 'Option2', '');
  // close the file

this is example of reading. Writing is easy to find out by ourself, having in mind that the TINIFile class have methods WriteInteger, WriteString, etc.

Hope this helps.
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Expert Comment

ID: 11794136
if IniFileName is NOT a full path name, TIniFile will search it in windows home directory (such as c:\windows, windows9x/me/xp or c:\winnt , win2000) by default. if the file exist then open else create it automatically.

LVL 12

Expert Comment

ID: 11803820
adayuer, what you said is not really true...

INI file can be in any folder you wish. Actually the application search for it in the current folder and folders in PATH. That's why it better to give the full path. Because if you say just CONFIG.INI and it is in your application folder, when the current directory is changed, it won't be found anymore.

That's why giving the full path will save you troubles. Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there.

Expert Comment

ID: 11805132
to Ivanov G:

hi, I found some source code in Delphi Source Code like this:

TCustomIniFile = class(TObject)
    FFileName: string;
    constructor Create(const FileName: string);
    property FileName: string read FFileName;

constructor TCustomIniFile.Create(const FileName: string);
  FFileName := FileName;

TIniFile = class(TCustomIniFile)
    destructor Destroy; override;
    procedure UpdateFile; override;

destructor TIniFile.Destroy;
  inherited Destroy;

procedure TIniFile.UpdateFile;
  WritePrivateProfileString(nil, nil, nil, PChar(FFileName));

then I found "WritePrivateProfileString" detail information in MSDN, like this:

BOOL WritePrivateProfileString(
  LPCTSTR lpAppName,
  LPCTSTR lpKeyName,
  LPCTSTR lpString,
  LPCTSTR lpFileName

about lpFileName, in Remarks section, there have some words : "If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.

If lpFileName contains a full path and file name and the file does not exist, WritePrivateProfileString creates the file. The specified directory must already exist. "

ok ,all things became clear, the core of this question is TIniFile.UpdateFile  procedure. this procedure call the win api "WritePrivateProfileString" , TIniFile.Create()'s paramter is the WritePrivateProfileString's last paramter - lpFileName.
"WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory." is my answer's base.

for more information, I wrote code :

   ini : TIniFile ;
   ini := TIniFile('test.ini');
   ini.WriteString('test', 'name', 'value') ;
   ini.Free ;

when i run these codes, i found the file "test.ini" in Windows Home Direcotry, i removed "test.ini" from Windows Home Directory to my App Current Directory. then i change my code :
   ini.WriteString('test', 'name', 'value1') ;
run  again,  I found new "test.ini" created in Windows Home Directory, it's content :

i open "test.ini" in my app Directory, the content:

the application do NOT search for the ini file in OTHER folder(s) except Windows Directory otherwise you give it a full  path.

I agree with you about "Putting you configuration files in Windows folder is a little shitty, since WINDOWS folder don't belong to you. Thus you are making garbage there."

08 15, 2004
Regards, adayuer


Expert Comment

ID: 11805154
God bless me, if you couldn't understand my poor english, am CRAZY...

good wishs to you.   :)

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Create Database on Android via Delphi dbExpress 3 90
Run video youtube webbrowse 10 61
Making delphi communicate with a c# service 16 103
When i run adoquery my application freezes 26 166
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question