Working with INI files in Delphi
How to use INI files in Delphi to store Application and User settings ?
What is INI file?
An INI file is a flat text file which stores data in a certain standard format. It is used to store light amounts of data, and is quite often used for storing application settings and other various data. It stores data in Sections, Key and Values format. An INI file can contain a number of unique Sections, and each of those Sections can contain a number of unique Keys with some Value. INI Files often use another file extension other than '.ini' for the developer's personalization of their projects. But INI file have size limit of 64KB. This is especially useful in cross-platform applications.
A Section is enclosed with [ and ] (for example, [MySection]). There cannot be more than one section with the same name within an INI file.
A Key is defined with an = following its Value. Each Key has its own line, underneath a Section.
For example INI file stores data in following format...
[Settings]
Width=200
Height=100
Left=0
Top=0
Background=1
[Files]
Background=C:\SomeFile.jpg
OkButton=C:\OKButtonFile.jpg
CancelButton=C:\CancelButtonFile.jpg
Settings, Files are Sections.
Width, Height are Keys and 200, 100 are values of respected keys.
How to read/write data from/to an INI file in Delphi?
To access INI file data Delphi provides 2 class TINIfile and TMemINIfile. Both classes provide same methods, functions to read/write data from/to an INI file.
TInifile
The TIniFile class is a wrapper of the Windows API for INI Files INI Files This does Unicode support, but only if Those Files are encoded as UTF-16
TMemIniFile
TMemIniFile class is a native Delphi implementation of INI file support. TMemIniFile enables buffered storage and retrieval of application-specific information and settings in an INI file. After the data is read, any changes to the data are stored in memory. To write the data from memory back to the associated INI file, call the UpdateFile method.
And that TMemInifile can use a bit more memory, but can be a bit faster if you re-read items repeatedly. If you are using small INI files, and/or you only read them once, there is almost no benefit to TMemInifile. I find TMemIniFile useful when I need to do some wonky thing like "walk through all the items in the file" and scan them (say, search all items, and do a partial text match for example
Delphi code Example...
uses inifiles;
...
Loading Form settings from INI file on Form create event.
procedure TMainForm.FormCreate(Sender: TObject) ;
var
appINI : TIniFile;
LastUser : string;
LastDate : TDateTime;
begin
appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
try //if no last user return an empty string
LastUser := appINI.ReadString('User','Last','') ; //if no last date return todays date
LastDate := appINI.ReadDate('User', 'Date', Date) ; //show the message
ShowMessage('This program was previously used by ' + LastUser + ' on ' + DateToStr(LastDate));
Top := appINI.ReadInteger('Placement','Top', Top) ;
Left := appINI.ReadInteger('Placement','Left', Left);
Width := appINI.ReadInteger('Placement','Width', Width);
Height := appINI.ReadInteger('Placement','Height', Height);
finally
appINI.Free;
end;
end;
Storing Form settings to INI file on Form close event.
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction) ;
var
appINI : TIniFile;
begin
appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;
try
appINI.WriteString('User','Last','Jitendra') ;
appINI.WriteDate('User', 'Date', Date) ;
with appINI, MainForm do
begin
WriteInteger('Placement','Top', Top) ;
WriteInteger('Placement','Left', Left) ;
WriteInteger('Placement','Width', Width) ;
WriteInteger('Placement','Height', Height) ;
end;
finally
appIni.Free;
end;
end;
Using of TMemINIFIle ....
var
appINI: TMemIniFile;
....
appIni := TMemIniFile.Create('test.ini', TEncoding.UTF8); // for Unicode //
Comments
Post a Comment