November 24, 2007

Reading and writting from TiniFile (to persists the data)

TIniFile is a low-level wrapper for the 16-bit Windows 3.x INI file system that enables storage and retrieval of application-specific information and settings in an application-specific INI file.

Unit
inifiles

Description
TIniFile enables handling the storage and retrieval of application-specific information and settings in a Windows 3.x INI file. The INI file text format is the standard way for Windows 3.x applications to store and retrieve application settings from session to session. An INI file stores information in logical groupings, called “sections.” For example, the WIN.INI file contains a section called “[Desktop]”. Within each section, actual data values are stored in named keys. Keys take the form:

[keynamegt]=[value]

The FileName passed to a TIniFile object when it is created is the name of the INI file the object accesses.

Note: Under 32-bit Windows systems, applications typically replace INI files with the system registry where all applications store and retrieve their settings. Delphi provides additional classes for handling the system registry. TRegistry encapsulates the system registry. TRegistryIniFile also encapsulates the registry, but shares a common ancestor with TIniFile so that the same code can access entries stored in either format.

Ex:

This example reads strings in the DELPHI32.INI file and displays them on the form.Before you run this example, you must add the IniFiles unit to the uses clause of your unit.

procedure TForm1.FormActivate(Sender: TObject);
var DelphiIni: TIniFile;

begin

Canvas.TextOut(20, 10, 'VARIOUS DELPHI SETTINGS');

DelphiIni := TIniFile.Create('Delphi32.Ini');

with DelphiIni do begin

with Canvas do

begin

TextOut(10, 50, 'Editor Font = ' + ReadString('Editor', 'FontName', 'ERROR')); TextOut(10, 70, 'Search Path = ' + ReadString('Library', 'SearchPath', 'ERROR')); TextOut(10, 90, 'Component Library = ' +
ReadString('Library', 'ComponentLibrary', 'ERROR')); TextOut(10, 110, 'VBX Directory = ' + ReadString('VBX', 'VBXDir', 'ERROR'));

TextOut(10, 130, 'VBX Unit Directory = ' + ReadString('VBX', 'UnitDir', 'ERROR')); end;

end;

DelphiIni.Free;

end;

Ex2:

This example reads the Transfer section of the DELPHI32.INI file into a memo and changes one of the strings in the INI file when Button1 is clicked. When Button2 is clicked, the DELPHI32.INI file is restored to its initial state, using the values stored in the memo.Before you run this example, you must add the IniFiles unit to the uses clause of your unit.
Warning: Do not click button2 before you have clicked button1!

procedure TForm1.Button1Click(Sender: TObject);
var DelphiIni: TIniFile;

begin DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');

Memo1.Clear;

DelphiIni.ReadSectionValues('Transfer', Memo1.Lines);

if Memo1.Lines.Values['Title1'] <> 'Picture Painter' then

DelphiIni.WriteString('Transfer', 'Title1', 'Picture Painter');

DelphiIni.Free;

end;


procedure TForm1.Button2Click(Sender: TObject);
var DelphiIni: TIniFile;

begin
DelphiIni := TIniFile.Create('c:\windows\delphi32.ini');
{ if the entry wasn’t there before, delete it now }

if Memo1.Lines.Values['Title1'] = '' then

DelphiIni.DeleteKey('Transfer', 'Title1') { otherwise, restore the old value }

else

DelphiIni.WriteString('Transfer', 'Title1', Memo1.Lines.Values['Title1']);

DelphiIni.Free;

end;

No comments:

Post a Comment