Difference between revisions of "Using INI Files"

From Lazarus wiki
Jump to navigationJump to search
Line 2: Line 2:
 
==INI Files==
 
==INI Files==
  
===Over View===
+
===Basic Information===
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 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. This unit is found in the FCL.
  
===INI Files?===
+
===INI Files===
INI Files work with sections,keys and values. Example:
+
INI Files use brackets to include '''Sections'''. In these sections are keys and key values..
[Section]
+
Key's and values and separated through "equal" symbols (=) ... Section names are put inside square brackets ([Section])
Key=Value
+
INI Files aren't has popularly used as XML files are, this is because INI files don't handle large strings very well. 
  
Just like that...
+
===Example===
It is posible to have multiple keys and sections..
 
  
====Why?====
+
The first thing we shall do is a simple console Application.
INI files make it incredibly easy to read and work with data.
+
First thing you should do is create an INI file and call it '''DB.ini''' and put it in your C:\ drive. Containing a section called INIDB and the following keys and values:
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.
+
Author=Adam
 +
Pass=
 +
DBFile=C:\Money.dat
 +
 +
Now lets move on the the code..
 
<Delphi>
 
<Delphi>
Progam INIex;
+
Program Project1;
  
{$mode objfpc}
+
{$mode objfpc}{$H+}
  
 
Uses
 
Uses
SysUtils,INIFiles;{INIFiles = The unit with the INI Classes}
+
  Classes,SysUtils,INIFiles;
  
 
Var
 
Var
  IniF:TINIFile;//The class
+
  INI:TINIFile;
</Delphi>
+
Author,Pass,DBFile:String;
 +
PassEnter:String;
  
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
 
begin
   Writeln('Creating class');
+
   INI := TINIFile.Create('C:\DB.ini');
   IF(FileExists('someini.ini'))then
+
   Author := INI.ReadString('INIDB','Author','');
 +
  Pass := INI.ReadString('INIDB','Pass','');
 +
  DBFile := INI.ReadString('INIDB','DBFile','');
 +
  if Pass <> '' then
 
   begin
 
   begin
     Inif := TINIFile.Create('someini.ini');
+
     Writeln('Password Required');
     Writeln(INiF.ReadString('s1','Key1','');
+
    Repeat
   End else Writeln('File not found...');
+
      Readln(PassEnter);
 +
      if not PassEnter = Pass then Writeln('Wrong Password');
 +
    until(PassEnter = Pass);
 +
     Writeln('Correct Password');
 +
  end;
 +
  Writeln('Author : '+Author);
 +
  Writeln('File : '+DBFile);
 +
   Writeln('Password : '+Pass);
 
   Readln;
 
   Readln;
end.
+
end.    
 
</Delphi>
 
</Delphi>
 
 
===Objects to know===
 
===Objects to know===
 
In the TINIFile class there are many different Propertys, procedures and functions to be used.
 
In the TINIFile class there are many different Propertys, procedures and functions to be used.

Revision as of 22:10, 5 December 2010

العربية (ar) Deutsch (de) English (en) español (es) suomi (fi) français (fr) русский (ru) 中文(中国大陆)‎ (zh_CN)

INI Files

Basic Information

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. This unit is found in the FCL.

INI Files

INI Files use brackets to include Sections. In these sections are keys and key values.. Key's and values and separated through "equal" symbols (=) ... Section names are put inside square brackets ([Section]) INI Files aren't has popularly used as XML files are, this is because INI files don't handle large strings very well.

Example

The first thing we shall do is a simple console Application. First thing you should do is create an INI file and call it DB.ini and put it in your C:\ drive. Containing a section called INIDB and the following keys and values:

Author=Adam
Pass=
DBFile=C:\Money.dat

Now lets move on the the code.. <Delphi> Program Project1;

{$mode objfpc}{$H+}

Uses

 Classes,SysUtils,INIFiles;

Var

INI:TINIFile;
Author,Pass,DBFile:String;
PassEnter:String;

begin

 INI := TINIFile.Create('C:\DB.ini');
 Author := INI.ReadString('INIDB','Author',);
 Pass := INI.ReadString('INIDB','Pass',);
 DBFile := INI.ReadString('INIDB','DBFile',);
 if Pass <>  then
 begin
   Writeln('Password Required');
   Repeat
     Readln(PassEnter);
     if not PassEnter = Pass then Writeln('Wrong Password');
   until(PassEnter = Pass);
   Writeln('Correct Password');
 end;
 Writeln('Author : '+Author);
 Writeln('File : '+DBFile);
 Writeln('Password : '+Pass);
 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