Using INI Files

From Free Pascal wiki
Revision as of 05:08, 31 August 2010 by Chronos (talk | contribs)

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..

Why?

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.

Example

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

{$mode objfpc}

Uses

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

Var

IniF:TINIFile;//The class

</Delphi>

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:

[S1]

Key1=Hello World

[S2]

Key2=Worldly Hello

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

 Writeln('Creating class');
 IF(FileExists('someini.ini'))then
 begin
   Inif := TINIFile.Create('someini.ini');
   Writeln(INiF.ReadString('s1','Key1',);
 End else Writeln('File not found...');
 Readln;

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