1.Delphi是有创建INI文件的方法,可以单独创建再用 2. 它和TXT文件读取内容不同,Delphie有特别对INI文件处理的方法,详细的处理方法你可以查找帮助,索引为TINIFile. 下面是一个例子,从注册表读取路径,并有读取路径上找到Ini文件,并将Ini文件的Advance 部分中的DataBaseType等内容读进一个TStringList中去。 var vGobalIni: TIniFile; Reg: TRegistry; vServices: TStringList; begin try try Reg := TRegistry.Create; Reg.RootKey := HKEY_LOCAL_MACHINE; Reg.OpenKey('\SOFTWARE\WinTools', False); LocalPath := Reg.ReadString('LocalPath') finally Reg.CloseKey; Reg.Free; end; try Reg := TRegistry.Create; Reg.RootKey := HKEY_LOCAL_MACHINE; Reg.OpenKey(LocalWinPath, False); vOSPath := Reg.ReadString('SystemRoot'); finally Reg.CloseKey; Reg.Free; end; if (LocalPath = EmptyStr) or (Not DirectoryExists(LocalPath)) then begin MessageDlg('Unable to initialize ' + Application.Title + 'Because Local Path not found,' +'Application will terminate.', mtError, [mbOK],0); Halt(0); end; if LocalPath[Length(LocalPath)] <> '\' then LocalPath := LocalPath + '\'; if vOSPath[Length(vOSPath)] <> '\' then vOSPath := vOSPath +'\'; vGobalIni := TIniFile.Create(LocalPath + GobalINI); DBPath := vGobalIni.ReadString('Advance', 'DataPath', ''); DataBaseType := vGobalIni.ReadInteger('Advance', 'DataBaseType', 1); if DBPath[Length(DBPath)] <> '\' then DBPath := DBPath + '\'; vServices := TStringList.Create; vGobalIni.ReadSectionValues('Services', vServices); vServices.Sort; finally vGobalINI.Free; end;
var s:tstrings; begin s:=stringlist.create; if fileexists(extractfilepath(paramstr(0)+'user.ini')) then s.loadfromfile(extractfilepath(paramstr(0))+'user.ini') else s.savetofile(extractfilepath(paramstr(0))+'user.ini') end;
procedure TForm1.Button1Click(Sender: TObject); var GameIni: TIniFile; begin Gameini := TIniFile.Create('c:\fungame.ini'); with GameIni do begin WriteInteger('Postion','Form.left',form1.Left); WriteInteger('Postion','Form.top',form1.Top); WriteInteger('Postion','Form.Width',form1.Width); WriteInteger('Postion','Form.height',form1.Height ); Free; end;end;procedure TForm1.Button2Click(Sender: TObject); var GameIni:TIniFile; i:integer; begin memo1.Clear; GameIni:=TIniFile.Create('c:\fungame.ini'); i:=GameIni.ReadInteger('Postion','Form.Left',0); ShowMessage(inttostr(i)); GameIni.Free; end;
---- INI文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如VB、VC、VFP、Delphi等都提供了读写INI文件的方法,其中Delphi中操作INI文件,最为简洁,这是因为Delphi3提供了一个TInifile类,使我们可以非常灵活的处理INI文件。 一、有必要了解INI文件的结构:
;注释
[小节名]
关键字=值
...---- 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文件支持的字符串、整型数值、布尔值三种数据类型,TINIfiles类提供了三种不同的对象方法来读取INI文件中关键字的值。
--- 假设已定义变量vs、vi、vb分别为string、 integer、boolean类型。
vs:=myinifile.Readstring('小节名','关键字',缺省值);
vi:=myinifile.Readinteger('小节名','关键字',缺省值);
vb:=myinifile.Readbool('小节名','关键字',缺省值);--- 其中缺省值为该INI文件不存在该关键字时返回的缺省值。 五、写入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;九、一个实例
---- 下面用一个简单的例子(如图),演示了建立、读取、存贮INI文件的方法。myini.ini文件中包含有“程序参数”小节,和用户名称(字符串)、是否正式用户(布尔值)和已运行时间(整型值)三个关键字。程序在窗体建立读取这些数据,并在窗体释放时写myini.ini文件。 --- 附源程序清单
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;
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
filename:=ExtractFilePath(paramstr(0))+'myini.ini';
myinifile:=TInifile.Create(filename);
edit1.Text:= myinifile.readstring('程序参数','用户名称','缺省的用户名称');
edit2.text:= inttostr(myinifile.readinteger('程序参数','已运行时间',0));
checkbox1.Checked:= myinifile.readbool('程序参数','是否正式用户',False);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
myinifile.writestring('程序参数','用户名称',edit1.Text);
myinifile.writeinteger('程序参数','已运行时间',strtoint(edit2.text));
myinifile.writebool('程序参数','是否正式用户',checkbox1.Checked);
myinifile.Destroy;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
edit2.Text:=inttostr(strtoint(edit2.text)+1);
end;end.
2.在uses中调用IniFiles单元,
TiniFile类中定义了许多成员函数,这里介绍几个使用频率较高的成员函数: ⑴ Create() 函数定义为: constructor Create(const FileName: string);该函数建立TiniFile类的对象。参数FileName是要读写的初始化文件名。若读写的文件在Windows的目录里(如system.ini文件),则可以直接写文件名而不必指定路径,否则就必须指定路径(如d:\ucdos\rdfnt.ini)。如按以下规则在规定的目录中存在该文件,则打开该初始化文件;否则在规定的目录里创建该初始化文件。 ⑵ ReadSections() 过程定义为: procedure ReadSections(Strings: TStrings);该过程将从所建立的TiniFile类的对象(即与之关联的初始化文件)中读取所有的节点名(即用[]括号括起的那部分,如rdfnt.ini文件中的[True Type fonts list])存入字符串列表中。参数Strings即为字符串列表的变量名。 ⑶ ReadSectionValues() 过程定义为: procedure ReadSectionValues(const Section: string; Strings: TStrings); 该过程将参数Section的值所对应的节点(如rdfnt.ini文件中的[True Type fonts list])中的各个关键字(如ARIALBI.TTF)及其所含的值(如ARIALBI.TTF关键字值为67)读入参数Strings指明的字符串列表中。 ⑷ ReadSection()过程定义为: procedure ReadSection(const Section: string; Strings: TStrings);该过程将参数Section的值所对应的节点中的各个关键字读入参数Strings指明的字符串列表中。与ReadSectionValues()不同的是它没有读取各个关键字的对应值。 ⑸ ReadString() 函数定义为: function ReadString(const Section, Ident, Default: string): string;该函数返回以参数Section的值为节点名、参数Ident的值为关键字名所对应的关键字值(如[True Type fonts list]节中ARIALBI.TTF关键字的值为67)。当指定的节点或节内的关键字不存在时,则函数返回参数Default的缺省值。返回的值是一个字符串型数据。当指定节点中关键字值的数据类型不是字符串时,则可用ReadInteger()成员函数读取一个整型值,用ReadBool()成员函数读取一个布尔值。 ⑹ WriteString()过程定义为: procedure WriteString(const Section, Ident, Value: string);该过程将参数Section的值为节点名、参数Ident的值为关键字名的关键字值设置为参数Value的值。该过程设置的是字符串型数据。当指定节点和关键字均存在时,则用Value的值替代原值;如指定节点不存在,则在关联的初始化文件中自动增加一个节点,该节点的值为参数Section的值,并在该节点下自动增加一个关键字,关键字名为参数Ident的值,该关键字对应的值为参数Value的值;若节点存在,但关键字不存在,则在该节点下自动增加一个关键字,关键字名为参数Ident的值,该关键字对应的值为参数Value的值。若要设置整型值,可调用WriteInteger()成员函数;用WriteBool()成员函数设置布尔值。
详细方法可以看一下Delphi的帮助
2. 它和TXT文件读取内容不同,Delphie有特别对INI文件处理的方法,详细的处理方法你可以查找帮助,索引为TINIFile.
下面是一个例子,从注册表读取路径,并有读取路径上找到Ini文件,并将Ini文件的Advance 部分中的DataBaseType等内容读进一个TStringList中去。
var
vGobalIni: TIniFile;
Reg: TRegistry;
vServices: TStringList;
begin
try
try
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey('\SOFTWARE\WinTools', False);
LocalPath := Reg.ReadString('LocalPath')
finally
Reg.CloseKey;
Reg.Free;
end;
try
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg.OpenKey(LocalWinPath, False);
vOSPath := Reg.ReadString('SystemRoot');
finally
Reg.CloseKey;
Reg.Free;
end;
if (LocalPath = EmptyStr) or (Not DirectoryExists(LocalPath)) then
begin
MessageDlg('Unable to initialize ' + Application.Title + 'Because Local Path not found,'
+'Application will terminate.', mtError, [mbOK],0);
Halt(0);
end;
if LocalPath[Length(LocalPath)] <> '\' then LocalPath := LocalPath + '\';
if vOSPath[Length(vOSPath)] <> '\' then vOSPath := vOSPath +'\';
vGobalIni := TIniFile.Create(LocalPath + GobalINI);
DBPath := vGobalIni.ReadString('Advance', 'DataPath', '');
DataBaseType := vGobalIni.ReadInteger('Advance', 'DataBaseType', 1);
if DBPath[Length(DBPath)] <> '\' then DBPath := DBPath + '\';
vServices := TStringList.Create;
vGobalIni.ReadSectionValues('Services', vServices);
vServices.Sort;
finally
vGobalINI.Free;
end;
s:tstrings;
begin
s:=stringlist.create;
if fileexists(extractfilepath(paramstr(0)+'user.ini')) then
s.loadfromfile(extractfilepath(paramstr(0))+'user.ini')
else
s.savetofile(extractfilepath(paramstr(0))+'user.ini')
end;
var GameIni: TIniFile;
begin
Gameini := TIniFile.Create('c:\fungame.ini');
with GameIni do
begin WriteInteger('Postion','Form.left',form1.Left);
WriteInteger('Postion','Form.top',form1.Top);
WriteInteger('Postion','Form.Width',form1.Width);
WriteInteger('Postion','Form.height',form1.Height );
Free;
end;end;procedure TForm1.Button2Click(Sender: TObject);
var
GameIni:TIniFile;
i:integer;
begin
memo1.Clear;
GameIni:=TIniFile.Create('c:\fungame.ini');
i:=GameIni.ReadInteger('Postion','Form.Left',0);
ShowMessage(inttostr(i));
GameIni.Free;
end;