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.
http://www.cnblogs.com/toosuo/archive/2007/12/17/1001405.html
delphi读取ini文件的更多相关文章
- Delphi对ini文件的操作
一.INI文件的结构:; 注释[小节名]关键字=值 INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值. 值的类型有三种:字符串.整型数值和布尔值.其中字符串存贮在INI ...
- Delphi操作Ini文件
Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件 关键字1=值1 关键子2=值2INI文件允许有多个小节, ...
- C#读取ini文件的方法
最近项目用到ini文件,读取ini文件,方法如下: using System; using System.Collections.Generic; using System.Linq; using S ...
- delphi对ini文件的操作(转载 万一)
ini 文件操作记要(1): 使用 TIniFileunit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Gr ...
- delphi中ini 文件操作记要(1): 使用 TIniFile
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- VS VC 读取 INI文件
1.获取应程序同极目录下的config.ini路劲 void GetConfigFilePath(char *path,int len, char *file) { char module[256] ...
- C# 读取ini文件,读不出来原因
先赋上相关读取ini文件代码 public class INIHelper { public string inipath; [DllImport("kernel32")] pri ...
- C# 通过api函数GetPrivateProfileString读取ini文件,取不到值
通过api函数GetPrivateProfileString读取ini文件,取不到值,测试了好长时间,都不行 确认程序,ini文件都没有错误的情况,最后发现是ini文件编码的原因. 将ini文件的编码 ...
- C# 读取ini文件 百度问问学习文档
C# 读取ini文件 10 有多个section,现想读取整个ini文件和指定section下所有内容 补充: 发布答案可以,请对准题目啊,我不要指定节点的内容,我知道!我要的是读取指定区域的内容,假 ...
随机推荐
- Thermally driven workload scheduling in a heterogeneous multi-processor system on a chip
Various embodiments of methods and systems for thermally aware scheduling of workloads in a portable ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- Everything starts with a dream(A day has only 24 hours and these things take time,所以要抓紧)
There is the famous quote: "Everything starts with a dream" and many years ago, Michael Va ...
- Web开发标配--开发人员工具-JS调试
喜欢从业的专注,七分学习的态度. JS:全称JavaScript,Web中,js主要在两个地方: html的<script type="text/javascript"> ...
- UE4.5.0的Kinect插件(Plugin)<一>
声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/43193431 UE4 Plugin,在UE4的官网,放出了有个 ...
- Erlang与ActionScript3采用JSON格式进行Socket通讯
http://hideto.iteye.com/blog/235811 需要下载as3corelib来为ActionScript3处理JSON codec server.erl -module(ser ...
- c#面向对象 基础知识(转)
OOP技术按照现实世界的特点来管理复杂的事物,把它们抽象为对象,具有自己的状态和行为,通过对消息的反应来完成一定的任务.这种编程方法提供了非常强大的多样性,大大增加了代码的重用机会,增加了程序开发的速 ...
- 检索08- SQL语句中的go与use用法
GO 1. 作用:向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号.2. 语法:一批 Transact-SQL 语句 GO 如 Select 1 Select 2 ...
- Method for training dynamic random access memory (DRAM) controller timing delays
Timing delays in a double data rate (DDR) dynamic random access memory (DRAM) controller (114, 116) ...
- windows下Eclipse启动tomcat提示port已被占用 already in use
>netstat -ano | findstr 8009 TCP 127.0.0.1:8005 0.0.0.0:0 LISTENING ...