Using INI Files

From Lazarus wiki
Revision as of 18:26, 30 August 2010 by Cjaster (talk | contribs) (INI Files..)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

INI Files

Over View

INI files can be used to save basic user settings easily. With the INIfiles unit and the TINIFile class you can easily work with existing INI files... I will show you the basics of the TINIFile class.

INI Files?

INI Files work with sections,keys and values. Example: [Section] Key=Value

Just like that... It is posible to have multiple keys and sections..


INI files make it incredibly easy to read and work with data. Similar to XML files.. I think programmers should be well rounded with one or the other.. Or both.


So first things first.. Lets do a basic console application. <Delphi> Progam INIex;

{$mode objfpc}


SysUtils,INIFiles;{INIFiles = The unit with the INI Classes}


IniF:TINIFile;//The class


Something you have to know is that when using the TINIfile class you have to work with an existing INI file.. So open Note pad and create an INI file.. Lets use this as an example:


Key1=Hello World


Key2=Worldly Hello

Ok.. Now back to our code <Delphi> begin

 Writeln('Creating class');
   Inif := TINIFile.Create('someini.ini');
 End else Writeln('File not found...');

end. </Delphi>

Objects to know

In the TINIFile class there are many different Propertys,procedures and functions to be used.

CaseSensitive - This property allows you to say if the keys and sections are case sensitive or not.. by default they aren't..

ReadString - Has 3 constant statements. the first one is the section to search in. The second one is the key to look for. The third one is a default string incase the key and\or section searched for is not found.

WriteString has three constant statements too... The first is the section. The third is the key and the last is the Value. If the key and section exist already the Key will be over written with the new value..

ReadSections - Will allow you to to take the sections from the INI file and put them in a TStrings class(Or TStringList with the AS code)

DeleteKey - Remove an existing Key from a specific section

EraseSection Remove a section and all its data

There are more procedures and functions but this is basic..

Last words...

Here: [1] you can learn all about INI Files to.. Please, if you can put up more information about INI files in Pascal. Modify At Will