======================================================
注:本文源代码点此下载
======================================================
delphi中初始化.ini文件的读写
金林樵郭彩虹
在windows中的应用程序极大多数拥有自己的初始化文件,如powerbuilder、office及cstar等。因此初始化文件的读写是每个高级程序员必须掌握的技术。虽然初始化文件的读写也可用object pascal中的文本文件一样读写,但因初始化文件不同于一般的文本文件,它有自己固定的格式(见下面的初始化文件是ucdos中提供的rdfnt.ini文件),如果用文本文件的方式读写,不仅格式转换十分繁琐,且很容易出现错误,为了方便程序员读写初始化文件中的数据,delphi中向用户提供了一个tinifile类,通过tinifile类就可十分方便地读写初始化文件。
ucdos中rdfnt.ini文件的内容为:
[true type fonts directory]
dir=c:\windows\system
[true type fonts list]
arial.ttf=64
arialbd.ttf=65
ariali.ttf=66
arialbi.ttf=67
times.ttf=68
timesbd.ttf=69
timesi.ttf=70
timesbi.ttf=71
cour.ttf=72
courbd.ttf=73
couri.ttf=74
courbi.ttf=75
[use all true type fonts]
all=0
tinifile类不是一个delphi的部件,因此不能在delphi的vcl模板中找到,它在delphi 系统中的inifiles单元中定义,因此要使用tinifile类,必须在使用该类的单元文件中用usesinifiles指令明确地说明。
tinifile类中定义了许多成员函数,这里介绍几个使用频率较高的成员函数:
⑴create()
函数定义为:constructor create(const filename: string);
该函数建立tinifile类的对象。参数filename是要读写的初始化文件名。
若读写的文件在windows的目录里(如system.ini文件),则可以直接写文件名而不必指定路径,否则就必须指定路径(如d:\ucdos\rdfnt.ini)。
如按以下规则在规定的目录中存在该文件,则打开该初始化文件;否则在规定的目录里创建该初始化文件。
⑵readsections()
过程定义为:procedure readsections(strings: tstrings);
该过程将从所建立的tinifile类的对象(即与之关联的初始化文件)中读取所有的节点名(即用[]括号括起的那部分,如rdfnt.ini文件中的[true type fonts list])存入字符串列表中。参数strings即为字符串列表的变量名。
⑶readsectionvalues()
过程定义为:procedure readsectionvalues(const section: string; strings: tstrings);
该过程将参数section的值所对应的节点(如rdfnt.ini文件中的[true type fonts list])中的各个关键字(如arialbi.ttf)及其所含的值(如arialbi.ttf关键字值为67)读入参数strings指明的字符串列表中。
⑷readsection()
过程定义为:procedure readsection(const section: string; strings: tstrings);
该过程将参数section的值所对应的节点中的各个关键字读入参数strings指明的字符串列表中。与readsectionvalues()不同的是它没有读取各个关键字的对应值。
⑸readstring()
函数定义为:function readstring(const section, ident, default: string): string;
该函数返回以参数section的值为节点名、参数ident的值为关键字名所对应的关键字值(如[true type fonts list]节中arialbi.ttf关键字的值为67)。当指定的节点或节内的关键字不存在时,则函数返回参数default的缺省值。返回的值是一个字符串型数据。
当指定节点中关键字值的数据类型不是字符串时,则可用readinteger()成员函数读取一个整型值,用readbool()成员函数读取一个布尔值。
⑹writestring()
过程定义为:procedure writestring(const section, ident, value: string);
该过程将参数section的值为节点名、参数ident的值为关键字名的关键字值设置为参数value的值。该过程设置的是字符串型数据。
当指定节点和关键字均存在时,则用value的值替代原值;如指定节点不存在,则在关联的初始化文件中自动增加一个节点,该节点的值为参数section的值,并在该节点下自动增加一个关键字,关键字名为参数ident的值,该关键字对应的值为参数value的值;若节点存在,但关键字不存在,则在该节点下自动增加一个关键字,关键字名为参数ident的值,该关键字对应的值为参数value的值。
若要设置整型值,可调用writeinteger()成员函数;用writebool()成员函数设置布尔值。
知道了以上函数的作用,要建立或读写一个初始化文件就不难了。下面以一个实际例子说明初始化文件的读取方法,步骤如下:
⒈在需要读写初始化文件的窗体(form)上放置名为sectioncombobox、identcombobox的两个组合式列表框,其中sectioncombobox存放节点名,identcombobox存放所选择节点的关键字名。一个名为identvalueedit的输入框,存放对应关键字的值。名为cmdchang的修改命令钮可以用来修改关键字的值,修改后用名为cmdsave的存储命令钮将修改后的关键字的值存入关联的初始化文件。窗体对应的单元名设为iniunit,窗体名设为iniform,窗体布局如下图一所示:
⒉在iniunit单元的interface部分用uses inifiles;说明要引用的tinifile类所定义的单元名。并在变量说明部分定义tinifile类的对象,如
varinifile:tinifile;
⒊建立窗体的oncreate事件过程。使用tinifile类的create成员函数创建tinifile对象,用该对象读写d:\ucdos目录中的rdfnt.ini初始化文件,并将该初始化文件中的所有节点通过readsections() 成员函数读入sectioncombobox组合式列表框中,用readsection()成员函数将第一个节点中的所有关键字读入identcombobox 组合式列表框,用readstring()成员函数将第一个关键字的值送入identvalueedit输入框。
⒋建立sectioncombobox组合式列表框的onchange事件过程。当该选择列表框中的不同项目(即不同的节点名)时,用readsection()成员函数将选节点中的所有关键字读入identcombobox 组合式列表框,并用readstring()成员函数将第一个关键字的值送入identvalueedit输入框。
⒌建立identcombobox组合式列表框的onchange事件过程。当该选择列表框中的不同项目(即不同的关键字名)时,用readstring()成员函数将该关键字的值送入identvalueedit输入框。
⒍建立命令钮cmdchang的onclick事件过程。使identvalueedit输入框中的内容可以修改(不按该命令钮,identvalueedit输入框是不能修改的),并设置命令钮cmdsave有效,可以将修改后的关键字值存入关联的初始化文件中。
⒎建立命令钮cmdsave的onclick事件过程。如果关键字值已改变,则调用writestring()成员函数将修改后的关键字的值存盘。
⒏建立窗体的ondestroy事件过程。当窗体失效时,将建立的tinifile对象释放,以释放该对象所战用的系统资源。
至此,运行该工程,初始化文件的读写已能顺利进行。当然还可以使用erasesection()成员函数删除指定的节,也可用deletekey()成员函数删除指定的关键字,因篇幅有限,这里就不详细介绍了,有兴趣的可参考delphi的使用帮助。
下面是该单元的源程序代码:
unit iniunit;
interface
uses
windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
stdctrls, inifiles;
type
tiniform = class(tform)
sectioncombobox: tcombobox;
label1: tlabel;
cmdsave: tbutton;
cmdchang: tbutton;
identcombobox: tcombobox;
identvalueedit: tedit;
label2: tlabel;
label3: tlabel;
procedure formcreate(sender: tobject);
procedure sectioncomboboxchange(sender: tobject);
procedure formdestroy(sender: tobject);
procedure cmdchangclick(sender: tobject);
procedure cmdsaveclick(sender: tobject);
procedure identcomboboxchange(sender: tobject);
private
{ private declarations }
public
{ public declarations }
end;
var
iniform: tiniform;
{ delphi中通过tinifile类读写windows的初始化文件 }
inifile:tinifile;
implementation
{$r *.dfm}
procedure tiniform.formcreate(sender: tobject);
begin
{ 使用tinifile类的create成员函数建立tinifile对
象,该对象用来读写d:\ucdos目录中的rdfnt.ini文件,
如果读写的文件在windows的目录里(如system.ini),
则可以直接写文件名而不必指定路径 }
inifile:=tinifile.create('d:\ucdos\rdfnt.ini');
{ 将tinifile对象关联的初始化文件system.ini中的所
有节(即用[]括号括起的那部分)的节名送入下拉式组
合列表框sectioncombobox中 }
sectioncombobox.clear;
inifile.readsections(sectioncombobox.items);
{ 选择system.ini文件的第一个节名 }
sectioncombobox.itemindex:=0;
sectioncomboboxchange(sender);
cmdsave.enabled:=false;
end;
{ 将组合列表框inicombobox中所选择节中对应的各个
变量及对应的值送入多行文本编辑器inimemo中 }
procedure tiniform.sectioncomboboxchange(sender: tobject);
begin
identcombobox.clear;
inifile.readsection(sectioncombobox.text,
identcombobox.items);
identcombobox.itemindex:=0;
identcomboboxchange(sender);
end;
procedure tiniform.identcomboboxchange(sender: tobject);
begin
identvalueedit.enabled:=false;
{ 将选择的关键字值读入 }
identvalueedit.text:=
inifile.readstring(sectioncombobox.text,
identcombobox.text,'');
end;
procedure tiniform.cmdchangclick(sender: tobject);
begin
cmdsave.enabled:=true;
identvalueedit.enabled:=true;
identvalueedit.setfocus;
end;
procedure tiniform.cmdsaveclick(sender: tobject);
begin
if identvalueedit.modified then begin
inifile.writestring(sectioncombobox.text,
identcombobox.text,
identvalueedit.text);
end;
identvalueedit.enabled:=false;
cmdsave.enabled:=false;
end;
procedure tiniform.formdestroy(sender: tobject);
begin
inifile.free;{ 释放创建的对象 }
end;
end.
以上方法在windows 95下用delphi 3.0调试通过。
trackback: http://tb.blog.youkuaiyun.com/trackback.aspx?postid=3449
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/