Delphi 读取ini文件

(1) INI文件的结构: 
;这是关于INI文件的注释部分 
[节点] 
关键字=值 
... 
INI文件允许有多个节点,每个节点又允许有多个关键字, “=”后面是该关键字的值(类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,布尔真值用1表示,布尔假值用0表示)。注释以分号“;”开头。


(2) INI文件的操作 
1、 在Interface的Uses节增加IniFiles; 
2、 在Var变量定义部分增加一行:inifile:Tinifile;然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。 
3、 打开INI文件:inifile:=Tinifile.create('tmp.ini'); 
4、 读取关键字的值: 
a:=inifile.Readstring('节点','关键字',缺省值);// string类型 
b:=inifile.Readinteger('节点','关键字',缺省值);// integer类型 
c:=inifile.Readbool('节点','关键字',缺省值);// boolean类型 
其中[缺省值]为该INI文件不存在该关键字时返回的缺省值。 
5、 写入INI文件: 
inifile.writestring('节点','关键字',变量或字符串值); 
inifile.writeinteger('节点','关键字',变量或整型值); 
inifile.writebool('节点','关键字',变量或True或False); 
当这个INI文件的节点不存在时,上面的语句还会自动创建该INI文件。 
6、 删除关键字: 
inifile.DeleteKey('节点','关键字');//关键字删除 
inifile.EraseSection('节点');// 节点删除 
7、 节点操作: 
inifile.readsection('节点',TStrings变量);//可将指定小节中的所有关键字名读取至一个字符串列表变量中; 
inifile.readsections(TStrings变量);//可将INI文件中所有小节名读取至一个字符串列表变量中去。 
inifile.readsectionvalues('节点',TStrings变量);//可将INI文件中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变量中去。 
8、 释放:inifile.distory;或inifile.free;




unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus;
type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
uses Inifiles,unit2;  //操作INI文件时记得要uses Inifiles这个单元
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);//读ini文件
Var
  MyIni :Tinifile;
  glAppPath :string;
begin
  glAppPath := ExtractFilePath(Application.ExeName);//获取当前运行程序的路径
  MyIni := Tinifile.Create(glAppPath + 'myini.ini');
  Edit1.Text := MyIni.ReadString('Setting','名称','');
  Edit2.Text := MyIni.ReadString('Setting','地址','');
  Edit3.Text := MyIni.ReadString('Setting','电话','');
  MyIni.Free;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
   Form2 := TForm2.Create(self);
   Form2.ShowModal;
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
  MyIni :Tinifile;
  glAppPath, path :string;
begin
  glAppPath := ExtractFilePath(Application.ExeName);
  MyIni := Tinifile.Create(glAppPath + 'myini.ini');
  Edit1.Text := MyIni.ReadString('Setting','名称','');
  Edit2.Text := MyIni.ReadString('Setting','地址','');
  Edit3.Text := MyIni.ReadString('Setting','电话','');
  MyIni.Free;
  Path := ExtractFilePath(Application.ExeName);
end;
end.




unit Unit2;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
type
  TForm2 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    Label3: TLabel;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form2: TForm2;
implementation
uses Inifiles;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);//写ini文件
Var
   MyIni :Tinifile;
   glAppPath :string;
begin
  glAppPath := ExtractFilePath(Application.ExeName);
  MyIni := Tinifile.Create(glAppPath + 'myini.ini');
  MyIni.WriteString('Setting','名称',Edit1.text);
  MyIni.WriteString('Setting','地址',Edit2.text);
  MyIni.WriteString('Setting','电话',Edit3.text);
  MyIni.Free;
  Close;
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
  close;
end;
end.




注意事项:
1.读写INI文件时要引用Inifiles单元
 
 2. glAppPath := ExtractFilePath(Application.ExeName);获取当前运行程序的路径,记得要设置一下程序的输出路径才行否则程序路径是安装delphi的路径,设置方法:project->options->Directories/Coditionals的属性设为
Output directory :E:\prj\ini文件(即程序保存路径)
Unit output directory :E:\prj\ini文件(即程序保存路径)


 MyIni := Tinifile.Create(glAppPath + 'myini.ini');
 
3 ,myinin.ini结构如下 
[Setting] (属性组名)
名称(属性名)=123(值)
地址=456
电话=789


 
4.
利用Windows API函数WritePrivateProfileString 和GetPrivateProfileString可对.INI文件进行读写操作。其实,对.INI文件的读写完全可以利用Delphi的内置函数来实现。下面就介绍一些对.INI文件读写时相关的类及其属性方法。


1、TIniFile对象


以Delphi中,定义了一个TIniFile对象,将.INI文件封装在其中,并提供一些方法,专门用来对INI文件进行读写操作。如果在程序中要用到TIniFile类或其方法属性,就必须在程序单元的uses语句中手工加入对IniFiles单元的引用。


2、Create方法


该方法用来创建一个处理INI文件的TIniFile类型实例。


方法声明:constructor Create(const FileName:string);


参数说明:FileName指明待创建的INI文件的文件名;


注释:在使用TIniFile对象之前,必须先用此方法创建一个INI文件的实例。FileName中可以包含路径名,缺省时为Windows所在目录(一般就是C:\windows,对于Windows NT来说,则一般是c:\winnt)。用Create方法创建的实例,在使用完之后,调用Free方法释放内存。


3、ReadSection方法


该方法从INI文件中读出指定段的所有子键名,并存入Strings参数指定的字符串列表对象中。


方法声明:procedure ReadSection(const Section:string; Strings:TStrings);


参数说明:Section指明要读取段的段名;


Strings指明存放子键名的字符串列表;


注释:ReadSection方法仅读入指定段的所有子键名,但不读入子键的值。


4、ReadSections方法


该方法从INI文件中读取所有段名,并存入Strings参数指定的字符中列表中。


方法声明:procedure ReadSections(Strings:TStrings);


参数说明:Strings参数指明存放段名的字符串列表;


注释:ReadSections方法将INI文件中所有段的段名读出,存入一指定的字符串列表中,此字符串列表可以直接使用某个列表框的Items属性。


5、ReadSectionValues方法


该方法从INI文件中读入指定段的所有子键名及其键值,并存入Strings参数指定的字符串列表中。


方法声明:procedure ReadSectionValues(const Section:String; Strings:TStrings);


参数说明:Section指明要读取段的段名;


Strings指明存放段名的字符串列表;


注释:ReadSectionValues方法与ReadSection方法的区别在于后者仅读入子键名,面前者除了读取子键名之外,还读取该子键对应的键值。读入的子键名及键值在字符串列表中的存放方法与在文件中的显示方法一致,即"Key=Value"形式。


6、EraseSection方法


该方法删除INI文件中指定的一个整段。


方法声明:procedure EraseSection(const Section:string);


参数说明:Section指明待删除段的段名;


注释:EraseSection方法不仅删除指定段的段名,面且同时将该段的所有子键及键值删除。


7、DeleteKey方法


该方法删除指定段中的某个指定的子键。


方法声明:procedure DeleteKey(const Section,Key:string);


参数说明:Section指明待删除子键据段的段名;


Key指明待删除子键的键名;


注释:DeleteKey方法删除整个子键(包括键名和键值),也就是删除该子键所在的一行。


8、ReadBool方法


该方法读取指定段的某个子键的布尔值。


方法声明:function ReadBool(const Section,Key:string;Default:Boolean):Boolean;


参数说明:Section指明待读子键所在段的段名;


Key指明待读子键的键名;


Default参数指明缺省时的返回值。


注释:ReadBool方法用于读取一个子键的布尔型值,当键值为"1"时,返回True,键值为"0"时,返回False.


9、WriteBool方法


该方法向指定段的某个子键写入布尔值。


方法声明:procedure WriteBool(const Section, Key:string; Value:Boolean);


参数说明:Senction指明待写入子键所在段的段名;


Key参数指明待写入值的子键键名;


Value指明待写入的布尔值;


注释:WriteBool 方法用于写入一个子键的布尔值,当Value为"True"时,写入"1"。Value为"Flase"时,写入"0"。若在写入时,指定的段或键名不存在,则自动创建该段和键名。


10、ReadInteger方法


该方法读取指定段的某个子键的整型值。


方法声明:function ReadInteger(const Section,Key:string; Default:longint):longint;


注释:此方法与ReadBool方法类似,只是变量类型不同。


11、WriteInteger方法


该方法向指写段的某个子键写放整型值。


方法声明:procedure WriteInteger(const Section,Key:string; Value:longint);


注释:此方法与WriteBool方法类似,只是变量灰型不同。


12、ReadString方法


该方法读取指定段的某个子键的字符串型 值。


方法声明:function ReadString(const Section,Key:string; Default:string):string;


注释;此方法与ReadBool方法类似,只是变量类型不同。


13、WriteString方法


该方法向指写段的某个子键写入整型值。


方法声明:procedure WriteString(const Section,Key:string; Value:string);


注释:此方法与WriteBool方法类似,只是变量类型不同。


14、FileName属性


该属性指明被封装在TIniFile对象中的INI文件的文件名。


属性声明:property FileName:string;


注释:FileName属性是一个运行时的只读属性。


由一面的介绍,我们可以看到,强大的Delphi对INI文件的支持是非常全面的。我们在编写涉及此类操作的程序时,几乎无需使用Windows API函数
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值