delphi建立、读取、存贮INI文件的方法三

Delphi教程 2025-08-30

从.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从.INI文件中获取字符的方法

 varMyIni:TIniFile;

 begin

 MyIni:=TIniFile.Create(´WIN.INI´);//调用的文件名

 edit1.text:=MyIni.ReadString(´Desktop´,´Wallpaper´,´´);//取得字符

 end;

 向.INI文件中写入字符的方法

 varMyIni:TIniFile;

 begin

 MyIni:=TIniFile.Create(´WIN.INI´);//调用的文件名

 DelphiIni.WriteString(´Desktop´,´Wallpaper´,´c:/a.bmp´);

 end;