[转载] delphi中ini 文件操作记要(1): 使用 TIni…
2018-06-17 18:56:08来源:未知 阅读 ()
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;
List.LoadFromFile(Path);
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.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Delphi10.3的ListView学习(大图标) 2020-06-05
- Delphi10.3的SpeedButton/BitBtn学习 2020-06-05
- Delphi10.3状态栏上显示进度条/图片 2020-06-02
- Delphi10.3下自带系统托盘 TrayIcon的使用 2020-06-01
- delphi10.3安装使用mySQL 2020-05-31
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash