delphi ini文件操作
{
[Section_Name]
Key_Name1=Value1
Key_Name2=Value2
}
uses
IniFiles;
// Write values to a INI file
procedure TForm1.Button1Click(Sender: TObject);
var
ini: TIniFile;
begin
// Create INI Object and open or create file test.ini
ini := TIniFile.Create('c:\MyIni.ini'
try
// Write a string value to the INI file.
ini.WriteString('Section_Name'
// Write a integer value to the INI file.
ini.WriteInteger('Section_
// Write a boolean value to the INI file.
ini.WriteBool('Section_Name', 'Key_Name', True);
finally
ini.Free;
end;
end;
// Read values from an INI file
procedure TForm1.Button2Click(Sender: TObject);
var
ini: TIniFile;
res: string;
begin
// Create INI Object and open or create file test.ini
ini := TIniFile.Create('c:\MyIni.ini'
try
res := ini.ReadString('Section_Name', 'Key_Name', 'default value');
MessageDlg('Value of Section: ' + res, mtInformation, [mbOK], 0);
finally
ini.Free;
end;
end;
// Read all sections
procedure TForm1.Button3Click(Sender: TObject);
var
ini: TIniFile;
begin
ListBox1.Clear;
ini := TIniFile.Create('MyIni.ini');
try
ini.ReadSections(listBox1.
finally
ini.Free;
end;
end;
// Read a section
procedure TForm1.Button4Click(Sender: TObject);
var
ini: TIniFile;
begin
ini: = TIniFile.Create('WIN.INI');
try
ini.ReadSection('Desktop', ListBox1.Items);
finally
ini.Free;
end;
end;
// Read section values
procedure TForm1.Button5Click(Sender: TObject);
var
ini: TIniFile;
begin
ini := TIniFile.Create('WIN.INI');
try
ini.ReadSectionValues('
finally
ini.Free;
end;
end;
// Erase a section
procedure TForm1.Button6Click(Sender: TObject);
var
ini: TIniFile;
begin
ini := TIniFile.Create('MyIni.ini');
try
ini.EraseSection('My_Section')
finally
ini.Free;
end;
end;
如何操作INI文件?
INI 文件在系统配置及应用程序参数保存与设置方面,
一、有必要了解 INI 文件的结构:
;注释
[小节名]
关键字=值
...
INI文件允许有多个小节,每个小节又允许有多个关键字,“=”
值的类型有三种:字符串、整型数值和布尔值。
注释以分号“;”开头。
二、定义
1、在Interface的Uses节增加IniFiles;
2、在Var变量定义部分增加一行:
myinifile:Tinifile;
然后,就可以对变量myinifile进行创建、打开、读取、
三、打开INI文件
myinifile:=Tinifile.create('
上面这一行语句将会为变量myinifile与具体的文件pro
值得注意的是,如果括号中的文件名没有指明路径的话,
把Program.
Filename:=ExtractFilePath(
四、读取关键字的值
针对INI文件支持的字符串、整型数值、布尔值三种数据类型,
假设已定义变量vs、vi、vb分别为string、
vs:=myinifile.Readstring ('小节名','关键字',缺省值);
vi:=myinifile.Readinteger ('小节名','关键字',缺省值);
vb:=myinifile.Readbool ('小节名','关键字',缺省值);
其中缺省值为该INI文件不存在该关键字时返回的缺省值。
五、写入INI文件
同样的,TInifile类也提供了三种不同的对象方法,
myinifile.writestring('小节名','
myinifile.writeinteger('小节名','
myinifile.writebool('小节名','
当这个INI文件不存在时,
六、删除关键字
除了可用写入方法增加一个关键字,
myinifile.DeleteKey('小节名','
七、小节操作
增加一个小节可用写入的方法来完成,
myinifile.EraseSection('小节名');
另外Tinifile类还提供了三种对象方法来对小节进行操作:
myinifile.readsection('小节名' TStrings变量);
myinifile.readsections(
myinifile.readsectionvalues('
八、释放
在适当的位置用下面的语句释放myinifile:
myinifile.distory;
九、一个实例
下面用一个简单的例子(如图),演示了建立、读取、
附源程序清单
unit Unit1;
interface
uses
Windows,Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, inifiles, StdCtrls, ExtCtrls;
type
TForm1=class(TForm)
Edit1:TEdit;
CheckBox1:TCheckBox;
Edit2:TEdit;
Label1:TLabel;
Label2:TLabel;
Timer1:TTimer;
Label3:TLabel;
procedureFormCreate(Sender:
procedureFormDestroy(Sender:
procedureTimer1Timer(Sender:
private
{Privatedeclarations}
public
{Publicdeclarations}
end;
var
Form1:TForm1;
implementation
var
myinifile:TInifile;
{$R*.DFM}
procedureTForm1.FormCreate(
var
filename:string;
begin
filename:=ExtractFilePath(
myinifile:=TInifile.Create(
edit1.Text:=myinifile.
edit2.text:=inttostr(
checkbox1.Checked:=myinifile.
end;
procedureTForm1.FormDestroy(
begin
myinifile.writestring('程序参数','
myinifile.writeinteger('程序参数',
myinifile.writebool('程序参数','
myinifile.Destroy;
end;
procedureTForm1.Timer1Timer(
begin
edit2.Text:=inttostr(strtoint(
end;
end.