Using INI Files/fr

From Free Pascal wiki
Revision as of 22:35, 17 April 2014 by E-ric (talk | contribs) (Created page with "{{INI Files}} ==INI Files== ===Information basique=== Les fichiers INI peuvent être utilisés pour enregistrer facilement des réglages utilisateur simples. Avec l'unité ''...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

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

INI Files

Information basique

Les fichiers INI peuvent être utilisés pour enregistrer facilement des réglages utilisateur simples. Avec l'unité INIFiles et la classe TINIFile vous pouvez simplement travailler avec les fichiers INI. Cette unité se trouve dans la FCL.

Fichiers INI

Les fichiers INI utilise des crochets pour créer et marquer des sections, qui contiennent des clés et des valeurs de clé. Une clé et sa valeur correspondante sont séparées par le signe égal (Clé=Valeur).

Les noms de sections sont mis dans des crochets ([Section]).

Les commentaires sont généralement permis et sont marqués avec des point virgules en début de ligne. Comme les fichiers INI ne sont pas totalement normalisés, d'autres caractères sont aussi utilisés tels que #.

De nos jours, les fichiers XML sont souvent utilisés pour le stockage des chaînes au lieu des fichiers INI, parce que les fichiers INI ne manipulent pas bien les grandes chaînes.Pourtant, les fichiers (comme-)ini reste une manière dominante pour la configuration des réglage de Linux/Unix.

Un exemple de fichier ini:

; Commentaire. Début du fichier INI

; les lignes vides sont ignorées

; notez qu'aucune sectio n'a été définie.
Compiler=Delphi
; Key: Compiler
; Value: Delphi

[General]
; Ici commence la section General
Compiler=FreePascal
; Key Compiler and value FreePascal

Exemple de lecture de fichier Ini

The console application below shows how to read ini files. To test it one should create the following ini file with the name "C:\DB.ini". Edit it to so it contains a section called INIDB and the following keys and values:

[INIDB]
; Save as C:\DB.ini
Author=Adam
Pass=
DBFile=C:\Money.dat

Now let's move on the the code..

Program IniSample;

{$mode objfpc}{$H+}

Uses
  Classes,SysUtils,INIFiles;

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

begin
  // Create the object, specifying the place where it can find the ini file:
  INI := TINIFile.Create('C:\DB.ini');
  // Demonstrates reading strings from the INI file.
  // You can also read booleans etc.
  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;
  // After we used ini file, we must call the Free method of object
  // ... although this really should be wrapped in a try..finally block
  // so that the object will be freed regardless of any errors above.
  Ini.Free; 
end.

Objects to know

In the TINIFile class there are many different properties, procedures and functions that can 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 in case the key and/or section searched for is not found.

WriteString - has three constant statements, too. The first is the section. The second is the key and the last is the value that you want to write. If the key and section exist already, the key will be overwritten 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 operator).

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 enough to get you started.

Reference Documentation

Here: Free Pascal documentation on INI files

Voir aussi