Difference between revisions of "Using INI Files/zh CN"

From Free Pascal wiki
Jump to navigationJump to search
m (Fixed syntax highlighting; deleted category included in page template)
 
(7 intermediate revisions by 2 users not shown)
Line 14: Line 14:
 
如今,XML文件经常被用来替代INI文件用作字符串的储存,因为INI文件对较长字符串的支持并不是太好。但是INI(类INI)文件依旧保持着Linux/Unix下设置文件的统治地位
 
如今,XML文件经常被用来替代INI文件用作字符串的储存,因为INI文件对较长字符串的支持并不是太好。但是INI(类INI)文件依旧保持着Linux/Unix下设置文件的统治地位
  
An example ini file:
+
一个INI文件的简单例子:  
 
 
 
<syntaxhighlight lang="ini">
 
<syntaxhighlight lang="ini">
; Comment. Beginning of INI file
+
; INI文件开头的注释
 
+
; empty lines are ignored
+
; 空行将会被忽略
 
+
; note that no section has been defined.
+
; 注意这里并没有定义任何的类目
 
Compiler=Delphi
 
Compiler=Delphi
; Key: Compiler
+
; : Compiler
; Value: Delphi
+
; : Delphi
 
+
 
[General]
 
[General]
; This starts a General section
+
; 这个标记表示General类目的开始
 
Compiler=FreePascal
 
Compiler=FreePascal
; Key Compiler and value FreePascal
+
; 键是 Compiler 且值是 FreePascal
 
</syntaxhighlight>
 
</syntaxhighlight>
  
===Ini file reading example===
+
===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:
+
下面的命令行程序展示了如何读取INI文件,为了测试这个程序我们必须建立如下的INI文件并保存为“C:\DB.ini”。编辑INI文件使它拥有一个叫做"INIDB"的类目以及如下的键与值:
  
 
<syntaxhighlight lang="ini">
 
<syntaxhighlight lang="ini">
 
[INIDB]
 
[INIDB]
; Save as C:\DB.ini
+
; 保存为 C:\DB.ini
 
Author=Adam
 
Author=Adam
 
Pass=
 
Pass=
Line 44: Line 43:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   
 
   
Now let's move on the the code..
+
现在让我们回到代码
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
Program IniSample;
 
Program IniSample;
  
 
{$mode objfpc}{$H+}
 
{$mode objfpc}{$H+}
 
+
 
Uses
 
Uses
 
   Classes,SysUtils,INIFiles;
 
   Classes,SysUtils,INIFiles;
 
+
 
Var
 
Var
 
  INI:TINIFile;
 
  INI:TINIFile;
 
  Author,Pass,DBFile:String;
 
  Author,Pass,DBFile:String;
 
  PassEnter:String;
 
  PassEnter:String;
 
+
 
begin
 
begin
   // Create the object, specifying the place where it can find the ini file:
+
   // 建立INI文件并指定INI文件的位置
 
   INI := TINIFile.Create('C:\DB.ini');
 
   INI := TINIFile.Create('C:\DB.ini');
   // Demonstrates reading strings from the INI file.
+
   // 从INI文件读取字符串的实例
   // You can also read booleans etc.
+
   // 你也可以读取想Boolean这样别的值
 
   Author := INI.ReadString('INIDB','Author','');
 
   Author := INI.ReadString('INIDB','Author','');
 
   Pass := INI.ReadString('INIDB','Pass','');
 
   Pass := INI.ReadString('INIDB','Pass','');
Line 79: Line 78:
 
   Writeln('Password : '+Pass);
 
   Writeln('Password : '+Pass);
 
   Readln;
 
   Readln;
   // After we used ini file, we must call the Free method of object
+
   // 在使用完INI文件后,你必须调用对象的Free方法
   // ... although this really should be wrapped in a try..finally block
+
   // 虽然这应该在try...finally块中...
   // so that the object will be freed regardless of any errors above.
+
   // 因为这个对象可能因为上述语句的一些错误而过早释放
 
   Ini.Free;  
 
   Ini.Free;  
end.    
+
end.</syntaxhighlight>
</syntaxhighlight>
+
 
 +
===对象百科===
 +
在TINIFile类中有许多不同的属性、过程和函数可以使用。
 +
 
 +
'''CaseSensitive''' - 这个属性允许你设置对于键和类目的名称是否大小写敏感,默认不敏感。
  
===Objects to know===
+
'''ReadString''' - 拥有三个参数:第一个是需要搜索的类目,第二个是要搜索的键,第三个是当类目或键未找到时返回的默认值
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.
+
'''WriteString''' - 同样拥有三个参数:第一个是类目,第二个键,第三个是要写入的值,如果键或类目已经存在,旧的数值会被新的数值覆盖。
  
'''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''' - 它允许你导出INI文件中所有的类目并放入TString类的实例中(或使用AS操作符转换为TStringList)
  
'''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.
+
'''DeleteKey''' - 从一个指定的类目中删除一个已经存在的键。
  
There are more procedures and functions but this is enough to get you started.
+
'''EraseSection''' - 删除一个类目以及其下所有的数据(键与值)。
  
===Reference Documentation===
+
除此之外还有许多其他的过程或函数,但这些足够使你开始。
Here: [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html Free Pascal documentation on INI files]
 
  
== See also ==
+
===参考文档===
 +
这: [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html FP中关于INI文件的文档]
  
* [[XML Tutorial]]
+
== 相关链接 ==
  
[[Category:Tutorials]]
+
* [[XML Tutorial/zh_CN]]
[[Category:FPC]]
 

Latest revision as of 08:09, 4 March 2020

العربية (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下设置文件的统治地位

一个INI文件的简单例子:

; INI文件开头的注释
 
; 空行将会被忽略
 
; 注意这里并没有定义任何的类目
Compiler=Delphi
; 键: Compiler
; 值: Delphi
 
[General]
; 这个标记表示General类目的开始
Compiler=FreePascal
; 键是 Compiler 且值是 FreePascal

Ini 文件读取实例

下面的命令行程序展示了如何读取INI文件,为了测试这个程序我们必须建立如下的INI文件并保存为“C:\DB.ini”。编辑INI文件使它拥有一个叫做"INIDB"的类目以及如下的键与值:

[INIDB]
; 保存为 C:\DB.ini
Author=Adam
Pass=
DBFile=C:\Money.dat

现在让我们回到代码

Program IniSample;

{$mode objfpc}{$H+}
 
Uses
  Classes,SysUtils,INIFiles;
 
Var
 INI:TINIFile;
 Author,Pass,DBFile:String;
 PassEnter:String;
 
begin
  // 建立INI文件并指定INI文件的位置
  INI := TINIFile.Create('C:\DB.ini');
  // 从INI文件读取字符串的实例
  // 你也可以读取想Boolean这样别的值
  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;
  // 在使用完INI文件后,你必须调用对象的Free方法
  // 虽然这应该在try...finally块中...
  // 因为这个对象可能因为上述语句的一些错误而过早释放
  Ini.Free; 
end.

对象百科

在TINIFile类中有许多不同的属性、过程和函数可以使用。

CaseSensitive - 这个属性允许你设置对于键和类目的名称是否大小写敏感,默认不敏感。

ReadString - 拥有三个参数:第一个是需要搜索的类目,第二个是要搜索的键,第三个是当类目或键未找到时返回的默认值 。

WriteString - 同样拥有三个参数:第一个是类目,第二个键,第三个是要写入的值,如果键或类目已经存在,旧的数值会被新的数值覆盖。


ReadSections - 它允许你导出INI文件中所有的类目并放入TString类的实例中(或使用AS操作符转换为TStringList)


DeleteKey - 从一个指定的类目中删除一个已经存在的键。

EraseSection - 删除一个类目以及其下所有的数据(键与值)。

除此之外还有许多其他的过程或函数,但这些足够使你开始。

参考文档

这: FP中关于INI文件的文档

相关链接