Using INI Files/zh CN

From Free Pascal wiki
Jump to navigationJump to search

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

INI 文件

基本信息

INI文件可以被方便地用来储存用户基本设置。通过INIfiles单元和TINIFile类,你可以轻松地使用已有的Ini文件。这个单元包括在FCL中。

INI 文件

INI文件使用方括号来创建类目,每个类目中有包含许多与其,一个键和与其对应的值使用等于号连接(key=Value)。

类目总是被包含在方括号里 ([Section]).

注释当然是允许的,只要在注释行的开头加上一个分号(;)就可以了。由于INI文件并没有统一的标准,所以其他的一些符号也有可能被使用,比如#。

如今,XML文件经常被用来替代INI文件用作字符串的储存,因为INI文件对较长字符串的支持并不是太好。但是INI(类INI)文件依旧保持着Linux/Unix下设置文件的统治地位

An example ini file:

; Comment. Beginning of INI file

; empty lines are ignored

; note that no section has been defined.
Compiler=Delphi
; Key: Compiler
; Value: Delphi

[General]
; This starts a General section
Compiler=FreePascal
; Key Compiler and value FreePascal

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

See also