Delphi ini文件操作 TIniFile、TMemIniFile
1、使用TIniFile
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IniFiles; {uses 包含 TIniFile 的单元}
var
ini: TIniFile;
path: string; {ini 文件路径}
Section,Key: string; {分别表示 ini 文件的小节与关键字}
{
ini文件结构:
;注释
[小节名]
关键字=值
INI文件支持: string、integer、boolean、Date、Time、DateTime、Double 与二进制类型
string 值没有引号
boolean 的真假用 1、0 表示
}
procedure TForm1.FormCreate(Sender: TObject);
begin
path := ChangeFileExt(ParamStr(0),'.ini');
ini := TIniFile.Create(path); {ini 对象建立需要文件路径参数, 如果缺少路径会默认Windows目录}
end;
//写入 ini 文件:
procedure TForm1.Button1Click(Sender: TObject);
begin
Section := 'AAA';
Key := 'AString';
ini.WriteString(Section,Key,'AAA-String');
Key := 'AInteger';
ini.WriteInteger(Section,Key,111);
Key := 'ABoolean';
ini.WriteBool(Section,Key,True);
Key := 'ADate';
ini.WriteDate(Section,Key,Now);
Key := 'ATime';
ini.WriteTime(Section,Key,Now);
Key := 'ADateTime';
ini.WriteDateTime(Section,Key,Now);
Key := 'ADouble';
ini.WriteFloat(Section,Key,Pi); Section := 'BBB';
Key := 'BString';
ini.WriteString(Section,Key,'BBB-String');
Key := 'BInteger';
ini.WriteInteger(Section,Key,222);
Key := 'BBoolean';
ini.WriteBool(Section,Key,True);
Key := 'BDate';
ini.WriteDate(Section,Key,Now);
Key := 'BTime';
ini.WriteTime(Section,Key,Now);
Key := 'BDateTime';
ini.WriteDateTime(Section,Key,Now);
Key := 'BDouble';
ini.WriteFloat(Section,Key,Pi); Section := 'CCC';
Key := 'CString';
ini.WriteString(Section,Key,'CCC-String');
Key := 'CInteger';
ini.WriteInteger(Section,Key,333);
Key := 'CBoolean';
ini.WriteBool(Section,Key,False);
Key := 'CDate';
ini.WriteDate(Section,Key,Now);
Key := 'CTime';
ini.WriteTime(Section,Key,Now);
Key := 'CDateTime';
ini.WriteDateTime(Section,Key,Now);
Key := 'CDouble';
ini.WriteFloat(Section,Key,Pi);
{写入结果:
[AAA]
AString=AAA-String
AInteger=111
ABoolean=1
ADate=2007-12-17
ATime=22:06:23
ADateTime=2007-12-17 22:06:23
ADouble=3.14159265358979
[BBB]
BString=BBB-String
BInteger=222
BBoolean=1
BDate=2007-12-17
BTime=22:06:23
BDateTime=2007-12-17 22:06:23
BDouble=3.14159265358979
[CCC]
CString=CCC-String
CInteger=333
CBoolean=0
CDate=2007-12-17
CTime=22:06:23
CDateTime=2007-12-17 22:06:23
CDouble=3.14159265358979
}
end;
//读取 ini 文件:
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
i: Integer;
b: Boolean;
f: Double;
d: TDate;
t: TTime;
dt: TDateTime;
begin
s := ini.ReadString('BBB','BString',''); {最后一个参数是默认值}
i := ini.ReadInteger('BBB','BInteger',0);
b := ini.ReadBool('BBB','BBoolean',False);
f := ini.ReadFloat('BBB','BDouble',0);
d := ini.ReadDate('BBB','BDate',Now);
t := ini.ReadTime('BBB','BTime',Now);
dt := ini.ReadDateTime('BBB','BDateTime',Now);
ShowMessage(s); {BBB-String}
ShowMessage(IntToStr(i)); {222}
ShowMessage(BoolToStr(b)); {-1(True)}
ShowMessage(FloatToStr(f)); {3.14159265358979}
ShowMessage(DateToStr(d)); {2007-12-17}
ShowMessage(TimeToStr(t)); {22:06:23}
ShowMessage(DateTimeToStr(dt)); {2007-12-17 22:06:23}
end;
//读入所有小节名到 TStrings:
procedure TForm1.Button3Click(Sender: TObject);
var
List: TStrings;
begin
List := TStringList.Create;
ini.ReadSections(List);
ShowMessage(List.Text);
{
AAA
BBB
CCC
}
List.Free;
end;
//读入指定小节的所有关键字到 TStrings:
procedure TForm1.Button4Click(Sender: TObject);
var
List: TStrings;
begin
List := TStringList.Create;
ini.ReadSection('AAA',List);
ShowMessage(List.Text);
{
AString
AInteger
ABoolean
ADate
ATime
ADateTime
ADouble
}
List.Free;
end;
//读入指定小节的所有关键字与值到 TStrings:
procedure TForm1.Button5Click(Sender: TObject);
var
List: TStrings;
begin
List := TStringList.Create;
ini.ReadSectionValues('BBB',List);
ShowMessage(List.Text);
{
BString=BBB-String
BInteger=222
BBoolean=1
BDate=2007-12-17
BTime=22:06:23
BDateTime=2007-12-17 22:06:23
BDouble=3.14159265358979
}
List.Free;
end;
//删除与添加
procedure TForm1.Button6Click(Sender: TObject);
begin
ini.DeleteKey('BBB','BString'); {删除关键字}
ini.EraseSection('CCC'); {删除小节}
// ini.UpdateFile; {保存到文件}
{添加小节与关键字或修改值, 直接写入即可}
end;
//其他功能
procedure TForm1.Button7Click(Sender: TObject);
var
b: Boolean;
s: string;
begin
b := ini.SectionExists('DDD'); {判断某个小节是否存在}
b := ini.ValueExists('AAA','AString'); {判断某个关键字的值是否存在}
s := ini.FileName; {获取文件名}
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ini.Free;
end;
end.
2、使用 TMemIniFile
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
IniFiles;
var
ini: TMemIniFile;
procedure TForm1.FormCreate(Sender: TObject);
begin
ini := TMemIniFile.Create('c:\temp\test.ini');
end;
//写入
procedure TForm1.Button1Click(Sender: TObject);
begin
ini.WriteString('AAA','A1','AAA-String');
//其他也都和 IniFile 一样使用
// ini.WriteInteger();
// ini.WriteBool();
// ini.WriteDate();
// ini.WriteTime();
// ini.WriteDateTime();
// ini.WriteFloat();
// ini.WriteBinaryStream();
//ini.UpdateFile; //因为 TMemIniFile 是内存操作, 这样才能保存到文件
end;
//读出及其他
procedure TForm1.Button2Click(Sender: TObject);
var
s: string;
begin
s := ini.ReadString('AAA','A1','默认值');
ShowMessage(s); //AAA-String
//其他读入命令也都和 IniFile 一样使用
// ini.ReadInteger();
// ini.ReadBool();
// ini.ReadDate();
// ini.ReadTime();
// ini.ReadDateTime();
// ini.ReadFloat();
// ini.ReadBinaryStream();
//还有四个常用方法也是和 IniFile 一样的
//ini.DeleteKey();
//ini.EraseSection();
//ini.ReadSection();
//ini.ReadSections();
//另外有三个 IniFile 中没有的方法也容易使用
//ini.GetStrings(List: TStrings);
//ini.SetStrings(List: TStrings);
//ini.Rename(const FileName: string; Reload: Boolean);
//其中 Rename 中的第二个 Boolean 参数如果为 True 将会刷新读入
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ini.Free;
end;
end.
Delphi ini文件操作 TIniFile、TMemIniFile的更多相关文章
- ini 文件操作记要(1): 使用 TIniFile
ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Class ...
- delphi INI文件
INI 文件读写 filecreate('路径加文件名')://创建一个文件. (1) INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个 ...
- delphi关于文件操作集锦
关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...
- ini文件操作
Config.ini 文件操作 [SYS] sysname=hy company=hyhy tel=2 using System; using System.Collections.Generic; ...
- winform INI文件操作辅助类
using System;using System.Runtime.InteropServices;using System.Text; namespace connectCMCC.Utils{ // ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- delphi中ini 文件操作记要(1): 使用 TIniFile
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Delphi INI 文件读写
delphi中,配置文件的相关操作. () INI文件的结构: ;这是关于INI文件的注释部分 [节点] 关键字=值 ... INI文件允许有多个节点,每个节点又允许有多个关键字, “=”后面是该关键 ...
- Delphi ini文件读写
参考:http://www.cnblogs.com/zhangzhifeng/archive/2011/12/01/2270267.html 一.ini文件的结构 ;这是关于 ini 文件的注释 [节 ...
随机推荐
- Mac OS 10.15系统入门教程 系统语言输入法详解
对于一些Mac新手来说呢还不知道偏好设置到底是什么?有什么用处?其实Mac系统内的几乎所有的系统相关的设置都会在系统偏好设置内出现. 切换系统语⾔在语言与地区设置中拖拽左侧的语言条目就可以切换系统的语 ...
- HTTP协议缓存
缓存的概念 缓存这个东西真的是无处不在, 有浏览器端的缓存, 有服务器端的缓存,有代理服务器的缓存, 有ASP.NET页面缓存,对象缓存. 数据库也有缓存, 等等. http中具有缓存功能的是浏览器缓 ...
- PHP 利用 curl 发送 post get del put patch 请求
因为需要在 php 开发中对接其它接口需要用 php curl 去对接其它接口 我把他们封装成函数 希望能对大家有所帮助. 这里面是封装好的会自动把 data 进行转成 json 格式,同时解码成 p ...
- 【Elasticsearch】Elasticsearch索引的创建、查看及修改
转: 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/liuxiao723846/art ...
- flutter图片组件
在flutter中,image组件有很多构造函数,常用的包括Image.asset(本地图片)和Image.network(远程图片). 常用属性 不管是显示本地图片还是远程图片,image组件都包含 ...
- DZY Loves Chemistry
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- [信息学奥赛一本通oj1741]电子速度 题解
对于$100 \%$的数据,$1≤n,m≤1e6 \ \ \ 0<=x_i,y_i<20170927 \ \ \ 1≤l_i,r_i≤n $ $Solution:$ 一开始没看懂题.后来大 ...
- 听说你懂个J?——前端发展闲聊
刚好周末和朋友聊起"前端从受鄙视到变得重要"这个话题,感慨前端这四年来的发展,遂有本文. 1. 前情提要 毋庸讳言,在我刚工作的时候,前端是还是一个不受重视的岗位.切图狗,写网页的 ...
- Spring Boot学习一之配置类及自动配置
一.配置类 1. 导入其他配置类 你不需要将所有的 @Configuration 放进一个单独的类, @Import 注解可以用来导入其他配置类.另外,你也可以使用 @ComponentScan 注解 ...
- JS 替换
JS 字符串有replace() 方法.但这个方法只会对匹配到的第一个字串替换. 如下例: var str = "wordwordwordword"; var strNew = s ...