myinifile.readsections(TStrings变量);可将INI文件中所有小节名读 取至一个字符串列表变量中去。
myinifile.readsectionvalues('小节名',TStrings变量);可将INI文件 中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变 量中去。 八、释放
在适当的位置用下面的语句释放myinifile: myinifile.distory; 九、一个实例
下面用一个简单的例子(如图),演示了建立、读取、存贮INI文件的方 法。myini.ini文件中包含有“程序参数”小节,和用户名称(字符串 )、是否正式用户(布尔值)和已运行时间(整型值)三个关键字。 程序在窗体建立读取这些数据,并在窗体释放时写myini.ini文件。 附源程序清单
unitUnit1; 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:TObject); procedureFormDestroy(Sender:TObject); procedureTimer1Timer(Sender:TObject); private
{Privatedeclarations} public
{Publicdeclarations} end; var
Form1:TForm1; implementation var
myinifile:TInifile; {$R*.DFM}
procedureTForm1.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;
procedureTForm1.FormDestroy(Sender:TObject); begin
myinifile.writestring('程序参数','用户名称',edit1.Text); myinifile.writeinteger('程序参数','已运行时间', strtoint(edit2.text));
myinifile.writebool('程序参数','是否正式用户', checkbox1.Checked); myinifile.Destroy; end;
procedureTForm1.Timer1Timer(Sender:TObject); begin
edit2.Text:=inttostr(strtoint(edit2.text)+1); end; end.
程序在Pwin95、Delphi3下调试通过。
---------------------------------------------------------------------- 演示程序如下:(注意:在 users 中必须包含 IniFiles) procedure TMainForm.DemoInfo; var
InfoFile:TIniFile;
UserName,UserClass:string; begin //创建对象
InfoFile:=TIniFile.Create('c:\\demo.ini'); //读字符,readstring(主键,键名,缺省值)
UserName:=InfoFile.ReadString('user','username','demo'); //写字符,writestring(主键,键名,键值) InfoFile.WriteString('user','age','18'); //删除键,DeleteKey(主键,键名) InfoFile.DeleteKey('class','userclass'); //删除主键,EraseSection(主键) InfoFile.EraseSection('class'); //释放对象 InfoFile.Free; end;
-------------------------------------------------------- 控制INI文件几种方法
在Windows中利用.INI文件做程序有关数据的存储工作是很常见的,其中涉及了读和写.INI文件问题,下面就介绍几种不同的方法给大家参考: 从.INI文件中获取字符串 var
strResult:pchar; begin
GetPrivateProfileString( 'windows', // []中标题的名字 'NullPort', // =号前的名字
'NIL', // 如果没有找到字符串时,返回的默认值 strResult, //存放取得字符 100, //取得字符的允许最大长度 'c:\\forwin95\\win.ini' // 调用的文件名 );
edit1.text:=strResult; //显示取得字符串 从.INI文件中获取整数
edit1.text:=inttostr(GetPrivateProfileInt( 'intl', // []中标题的名字 'iCountry', // =号前的名字
0,// 如果没有找到整数时,返回的默认值
'c:\\forwin95\\win.ini' // 调用的文件名 ));
向.INI文件写入字符串 WritePrivateProfileString( 'windows', // []中标题的名字 'load', // 要写入“=”号前的字符串 'accca', //要写入的数据
'c:\\forwin95\\win.ini' // 调用的文件名 );
向.INI文件写入整数 WritePrivateProfileSection( 'windows', // []中标题的名字 'read=100', // 要写入的数据 'c:\\forwin95\\win.ini' // 调用的文件名 );
上面的方法是调用API函数,下面介绍另一种不用API,而是使用TIniFile从.INI文件中获取字符的方法 从.INI文件中读字符 var MyIni: TIniFile; begin
MyIni := TIniFile.Create('WIN.INI');//调用的文件名
edit1.text:=MyIni.ReadString('Desktop', 'Wallpaper', '');//取得字符 end;
向.INI文件中写入字符 var MyIni: TIniFile; begin
MyIni := TIniFile.Create('WIN.INI');//调用的文件名
DelphiIni.WriteString('Desktop', 'Wallpaper', 'c:\\a.jpg');//写入字符 end;
下面的是本人自制的读INI文件函数,也提供给大家参考: function GetINIfile(lpAppNameL,lpKeyName,lpDefault:string; lpsize:integer;lpFileName:string):string; {读取ini文件函数} var f:textfile; sn:string; begin
assignfile(f,lpFileName); reset(f); repeat
readln(f,sn);
if sn='['+lpAppNameL+']' then begin readln(f,sn);
while(copy(sn,1,1)<>'[')or(not(eof(f)))do begin
if copy(sn,1,pos('=',sn)-1)=lpKeyName then begin
GetINIfile:=copy(sn,pos('=',sn)+1,lpsize); exit; end; readln(f,sn); end; end
else GetINIfile:=lpDefault; until eof(f); closefile(f); end; {------------} 调用方法是: var Timeout:String; begin
Timeout:=GetINIfile('MailSetup','Timeout','0',5,prgpath+'\\CNMSet.ini'); end;
------------------------------------------------------------- FileExists函数 判断文件是否存在
FPath:=ExtractFielPath(Paramstr(0))+'FName.ini' If Not FileExists('FPath') then
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库shellAPI 删除文件夹(2)在线全文阅读。
相关推荐: