delphi读取ini文件

ini文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如vb、vc、vfp、delphi等都提供了读写ini文件的方法,其中delphi中操作ini文件,最为简洁,这是因为delphi提供了一个tinifile类,使我们可以非常灵活的处理ini文件

一.ini文件的结构

[小节名]ini文件

关键字1=值1

关键子2=值2

ini文件允许有多个小节,每个小节又允许有多个关键字,“=”后面是该关键字的值。

值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在ini文件中时没有引号,布尔真值用1表示,布尔假值用0表示。

二、定义

1、 在interface的uses节增加inifiles;

2、 在var变量定义部分增加一行: myinifile:tinifile;

定义类的一个实例。然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。

三、打开ini文件

myinifile:=tinifile.create(program.ini);

上面这一行语句将会为变量myinifile与具体的文件program.ini建立联系,然后,就可以通过变量myinifile,来读写program.ini

文件中的关键字的值了。

值得注意的是,如果括号中的文件名没有指明路径的话,那么这个program.ini文件会存储在windows目录中,把program.ini文件存储在应用程序当前目录中的方法是:为其指定完整的路径及文件名。下面的两条语句可以完成这个功能:

filename:=extractfilepath(paramstr

(0))+program.ini;

myinifile:=tinifile.create(filename);

五、写入ini文件

同样的,tinifile类也提供了三种不同的对象方法,向ini文件写入字符串、整型数及布尔类型的关键字。


myinifile.writestring(小节名,关键字,变量或字符串值);
myinifile.writeinteger(小节名,关键字,变量或整型数值);
myinifile.writebool(小节名,关键字,变量或true或false);

当这个ini文件不存在时,上面的语句还会自动创建该ini文件。

六、删除关键字

除了可用写入方法增加一个关键字,tinifile类还提供了一个删除关键字的对象方法:


myinifile.deletekey(小节名,关键字);

七、小节操作

增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法:


myinifile.erasesection(小节名);

另外tinifile类还提供了三种对象方法来对小节进行操作:

myinifile.readsection(小节名,tstrings变量);可将指定小节中的所有关键字名读取至一个字符串列表变量中;

myinifile.readsections(tstrings变量);可将ini文件中所有小节名读取至一个字符串列表变量中去。

myinifile.readsectionvalues(小节名,tstrings变量);可将ini文件中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变量中去。

八、释放

在适当的位置用下面的语句释放myinifile:

myinifile.distory;

下面是具体例子。源代码如下。创建了一个myini.ini文件,有一个名为newini的小节,有3个关键字“用户名称”“已运行时间”“是否正式用户”。运行效果,可以在edit1里边填入“用户名称”;edit2显示时间,不可以改变数值;checkbox1通过打勾,保存时间和“用户名称”进入myini.ini文件里边,重新打开应用程序时,显示的时保存下来的时间和填入的“用户名称”,如果在myini.ini文件里边修改,效果和在程序运行过程中修改时一样的。

unit unit1;

interface

uses

windows, messages, sysutils, variants, classes, graphics, controls, forms,

dialogs,inifiles, stdctrls, extctrls;

{调用inifiles 类}

type

tform1 = class(tform)

label1: tlabel;

label2: tlabel;

label3: tlabel;

edit1: tedit;

edit2: tedit;

timer1: ttimer;

checkbox1: tcheckbox;

procedure formcreate(sender: tobject);

procedure formdestroy(sender: tobject);

procedure timer1timer(sender: tobject);

private

{ private declarations }

public

{ public declarations }

end;

var

form1: tform1;

implementation

var

myinifile:tinifile;

{定义一个类的实例}

{$r *.dfm}

procedure tform1.formcreate(sender: tobject);

var

filename:string;

begin

{下面两行的书写形式,在应用程序的路径下创建ini文件}

filename:=extractfilepath(paramstr(0))+'myini.ini';

myinifile:=tinifile.create(filename);

edit1.text:=myinifile.readstring('newini','用户名称','胡长浩');

edit2.text:=inttostr(myinifile.readinteger

('newini','已运行时间',0));

checkbox1.checked:=myinifile.readbool

('newini','是否正式用户',false);

{newini是小节名字,中间字段是关键字的名字,第三个字段是缺省值。当myini.ini不存在时,上面的语句自动创建这个文件,上边几行里的引号是单引号}

end;

procedure tform1.formdestroy(sender: tobject);

begin

myinifile.writestring('newini','用户名称',edit1.text);

myinifile.writeinteger('newini','已运行时间',

strtoint(edit2.text));

myinifile.writebool('newini','是否正式用户',

checkbox1.checked);

myinifile.destroy;

end;

procedure tform1.timer1timer(sender: tobject);

begin

edit2.text:=inttostr(strtoint(edit2.text)+1);

end;

end.