📄 h20000424-14.htm
字号:
<html><head><script language="JavaScript"> function reset(){ for ( var i=0; i < document.Form1.elements.length; i++ ) { document.Form1.elements[i].options[0].selected=true }}function going(url){ parent.location=url; reset();}</script><style><!--A:link {text-decoration: none; color: #000000; font-family: 宋体}A:visited {text-decoration: none; color: #000000; font-family: 宋体}A:active {text-decoration: none; font-family: 宋体}A:hover {text-decoration: underline:#ffffff; color: #ff0000}body,table {font-size: 9pt; font-family: 宋体}.H1 {font-size: 9pt ; line-height:9pt; align=center}.ourfont {font-size: 9pt ; line-height:9pt; }.ourfont1 {font-size: 9pt ; line-height:9pt; }--></style><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>Delphi中INI的编程</title></head><body topmargin="0" leftmargin="0"><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody> <tr> <td><p class="content4"><span class="font"><img border="0" height="3" src="../../soft/pix.gif" width="700"></span></td> </tr></tbody></table></center></div><div align="center"><center><table border="0" cellPadding="0" cellSpacing="0" width="700"><tbody> <tr> <td bgColor="#3399ff" width="658"><p class="ourfont"><span class="font"> </span><a style="text-decoration: none; color: rgb(255,255,255)" href="../os/H00705-2.htm">操作系统</a><font color="#FFFFFF"><span class="font"><span> |</span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../image/H00705-14.htm">图像处理</a><font color="#FFFFFF"><span>|<span class="font"> </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../autoCAD/H00705-19.htm">辅助设计</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../data/H00705-18.htm">数 据 库</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="H00706-1.htm">高级编程</a><span class="font"><font color="#FFFFFF"><span> | </span></font></span><a style="color: rgb(255,255,255); text-decoration: none" href="../netuse/H00705-20.htm">网络应用</a><font color="#FFFFFF"><span><span class="font"> | </span></span></font><a style="color: rgb(255,255,255); text-decoration: none" href="../webpage/H00705-21.htm">网页制作</a><font color="#FFFFFF"><span><span class="font"> |</span></span></font> <a style="color: rgb(255,255,255); text-decoration: none" href="../technic/H00706-3.htm">技术交流 </a><a style="color: rgb(255,255,255); text-decoration: none" href="http://202.100.13.77/clslyb/default.asp?clsid=jiaoc"><font color="#FFFFFF"><strong>过客留言</strong></font></a></td> <td bgColor="#3399ff" width="32"><p align="center" class="ourfont"><a href="mailto:net@163.sn.cn"><img src="../../soft/email.gif" alt="email.gif (493 字节)" border="0" WIDTH="20" HEIGHT="19"></a></td> </tr></tbody></table></center></div><div align="center"><center><table border="1" cellSpacing="1" width="700" bordercolor="#3399FF"><tbody> <tr> <td align="left" bgColor="#FFFFFF" vAlign="top" width="700" bordercolor="#0000FF" bordercolorlight="#0000FF" bordercolordark="#0000FF"><p class="ourfont"> </p> <p align="center">Delphi中的INI文件编程 <br> <br> 湖北省枝江市委办公室 曹祖权 </p> <p><br> ---- INI文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如VB、VC、VFP、Delphi等都提供了读写INI文件的方法,其中Delphi中操作INI文件,最为简洁,这是因为Delphi3提供了一个TInifile类,使我们可以非常灵活的处理INI<br> 文件。<br> 一、有必要了解INI文件的结构:<br> ;注释<br> [小节名]<br> 关键字=值<br> ...<br> ---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值。 <br> ---- 值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,布尔真值用1表示,布尔假值用0表示。 <br> ---- 注释以分号“;”开头。 <br> 二、定义<br> ---- 1、在Interface的Uses节增加IniFiles; <br> ---- 2、在Var变量定义部分增加一行: <br> myinifile:Tinifile;<br> ---- 然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。 <br> 三、打开INI文件<br> myinifile:=Tinifile.create(`program.ini`);<br> ---- 上面这一行语句将会为变量myinifile与具体的文件 program.ini建立联系,然后,就可以通过变量myinifile,来读写program.ini文件中的关键字的值了。 <br> ---- 值得注意的是,如果括号中的文件名没有指明路径的话,那么这个Program.ini文件会存储在Windows目录中,把Program.ini文件存储在应用程序当前目录中的方法是:为其指定完整的路径及文件名。下面的两条语句可以完成这个功能: <br> Filename:=ExtractFilePath(Paramstr<br> (0))+`program.ini`;<br> myinifile:=Tinifile.Create(filename);<br> 四、读取关键字的值<br> ---- 针对INI文件支持的字符串、整型数值、布尔值三种数据类型,TINIfiles类提供了三种不同的对象方法来读取INI文件中关键字的值。 <br> ---- 假设已定义变量vs、vi、vb分别为string、 integer、boolean类型。 <br> vs:=myinifile.Readstring<br> (`小节名`,`关键字`,缺省值);<br> vi:=myinifile.Readinteger<br> (`小节名`,`关键字`,缺省值);<br> vb:=myinifile.Readbool<br> (`小节名`,`关键字`,缺省值);<br> ---- 其中缺省值为该INI文件不存在该关键字时返回的缺省值。 <br> 五、写入INI文件<br> ---- 同样的,TInifile类也提供了三种不同的对象方法,向INI文件写入字符串、整型数及<br> 布尔类型的关键字。 <br> myinifile.writestring(`小节名`,`关键字`,变量或字符串值);<br> myinifile.writeinteger(`小节名`,`关键字`,变量或整型数值);<br> myinifile.writebool(`小节名`,`关键字`,变量或True或False);<br> ---- 当这个INI文件不存在时,上面的语句还会自动创建该INI文件。 <br> 六、删除关键字<br> ---- 除了可用写入方法增加一个关键字,Tinifile类还提供了一个删除关键字的对象方法: <br> myinifile.DeleteKey(`小节名`,`关键字`);<br> 七、小节操作<br> ---- 增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法: <br> myinifile.EraseSection(`小节名`);<br> ---- 另外Tinifile类还提供了三种对象方法来对小节进行操作: <br> ---- myinifile.readsection(`小节名`,TStrings变量);可将指定小节中的所有关键字名读<br> 取至一个字符串列表变量中; <br> ---- myinifile.readsections(TStrings变量);可将INI文件中所有小节名读取至一个字符<br> 串列表变量中去。 <br> ---- myinifile.readsectionvalues(`小节名`,TStrings变量);可将INI文件中指定小节的<br> 所有行(包括关键字、=、值)读取至一个字符串列表变量中去。 <br> 八、释放<br> 在适当的位置用下面的语句释放myinifile:<br> myinifile.distory;<br> 九、一个实例<br> ---- 下面用一个简单的例子(如图),演示了建立、读取、存贮INI文件的方法。myini.ini文件中包含有“程序参数”小节,和用户名称(字符串)、是否正式用户(布尔值)和已运行时间(整型值)三个关键字。程序在窗体建立读取这些数据,并在窗体释放时写myini.ini文件。 <br> ---- 附源程序清单 <br> unit Unit1;<br> interface<br> uses<br> Windows, Messages, SysUtils, Classes, Graphics, <br> Controls, Forms, Dialogs,inifiles,StdCtrls, ExtCtrls;<br> type<br> TForm1 = class(TForm)<br> Edit1: TEdit;<br> CheckBox1: TCheckBox;<br> Edit2: TEdit;<br> Label1: TLabel;<br> Label2: TLabel;<br> Timer1: TTimer;<br> Label3: TLabel;<br> procedure FormCreate(Sender: TObject);<br> procedure FormDestroy(Sender: TObject);<br> procedure Timer1Timer(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> var<br> Form1: TForm1;<br> <br> implementation<br> var<br> myinifile:TInifile;<br> {$R *.DFM}<br> <br> procedure TForm1.FormCreate(Sender: TObject);<br> var<br> filename:string;<br> begin<br> filename:=ExtractFilePath(paramstr(0))+`myini.ini`;<br> myinifile:=TInifile.Create(filename);<br> edit1.Text:= myinifile.readstring<br> (`程序参数`,`用户名称`,`缺省的用户名称`);<br> edit2.text:= inttostr(myinifile.readinteger<br> (`程序参数`,`已运行时间`,0));<br> checkbox1.Checked:= myinifile.readbool<br> (`程序参数`,`是否正式用户`,False);<br> end;<br> <br> procedure TForm1.FormDestroy(Sender: TObject);<br> begin<br> myinifile.writestring(`程序参数`,`用户名称`,edit1.Text);<br> myinifile.writeinteger(`程序参数`,`已运行时间`,<br> strtoint(edit2.text));<br> myinifile.writebool(`程序参数`,`是否正式用户`,<br> checkbox1.Checked);<br> myinifile.Destroy;<br> end;<br> <br> procedure TForm1.Timer1Timer(Sender: TObject);<br> begin<br> edit2.Text:=inttostr(strtoint(edit2.text)+1);<br> end;<br> <br> end.<br> ---- 程序在Pwin95、Delphi3下调试通过。 </td> </tr></tbody></table></center></div><hr align="center" SIZE="1" width="700"><p align="center" class="ourfont"><span><span><font color="#000000"> <a style="color: rgb(0,0,0); text-decoration: none" href="../../soft/network/index.htm">互联网络</font><font color="#FFFFFF"> </font></a><font color="#000000">|</span></span><a href="../../soft/system/index.htm" style="color: rgb(0,0,0); text-decoration: none">系统工具</a><span>|<span> <a href="../../soft/tools/index.htm" style="color: rgb(0,0,0); text-decoration: none">实用工具</a> | <a href="../../soft/multimedia/index.htm" style="color: rgb(0,0,0); text-decoration: none">媒体工具</a> | <a href="../../soft/image/index.htm" style="color: rgb(0,0,0); text-decoration: none">图形图像</a> | <a href="../../soft/game/index.htm" style="color: rgb(0,0,0); text-decoration: none">游戏娱乐</a> | </span><a href="../../soft/usesoft/index.htm" style="color: rgb(0,0,0); text-decoration: none">软件<span>使用</a></font><font color="#FFFFFF"> |</font> </span></span></p><hr align="center" SIZE="1" width="700"><font color="#FFE8BB"><p align="center" class="ourfont"></font><font color="#000000"><span><span>© Copyright 2000 </span></span>陕西公用计算机互联网信息中心<span><span> 版权所有 </font></p></span></span><p align="center" class="ourfont"><font color="#000000"><span><span>本站制作维护by <a style="color: rgb(0,0,0)" href="mailto:haiyuan@163.sn.cn">HAIYUAN</a></span></span></font></p><p align="center" class="ourfont"><font color="#000000"><span><span>电话:(029)8371051 8371055 8371049 联系信箱:<a href="mailto:net@163.sn.cn">net@163.sn.cn</a></span></span></font></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -