一,ini文件注释以‘;’开头
二,定义
1,在interface的uses节中添加inifiles;
2,var myinfile:tinifile;
3,打开ini文件
(1)myinifile:=tinifile.create('program.ini');
(2)var filename:string;
filename:=extractfilepath(paramstr(0))+'program.ini'; //0代表搜索到本项目的地址物理路径;
myinifile:=tinifile.create(filename);
//不指名路径的情况下文件会建在(c\windows目录中)
三,读取
procedure TForm2.btn2Click(Sender: TObject);
var
xiaojie:string;
guanjian:string;
begin
xiaojie:=edt1.Text;
guanjian:=edt2.Text;
if (xiaojie<>'') and (guanjian<>'') then
begin
if cbb2.ItemIndex=0 then
lbl6.Caption:=myinifile.ReadString(xiaojie,guanjian,缺省值) //缺省值代表ini不存在该关键字时返回值
else if cbb2.ItemIndex=1 then
bl6.Caption:=IntToStr(myinifile.ReadInteger(xiaojie,guanjian,0))
else if cbb2.ItemIndex=2 then
cbb1.ItemIndex:=Integer(myinifile.ReadBool(xiaojie,guanjian,False))
end;
myinifile.Free;
end;
四,写入
procedure TForm2.btn1Click(Sender: TObject);
var
xiaojie:string;
guanjian:string;
zhi:string;
zhi1:Integer;
zhi2:Boolean;
begin
xiaojie:=edt1.Text;
guanjian:=edt2.Text;
zhi:=edt3.Text;
zhi1:=StrToInt(edt4.Text);
zhi2:=Boolean(cbb1.ItemIndex);
if edt4.text<>'' then
myinifile.WriteString(xiaojie,guanjian,zhi);
if edt4.text<>'' then
myinifile.WriteInteger(xiaojie,guanjian,zhi1);
if (cbb1.ItemIndex=0) or (cbb1.ItemIndex=1) then
myinifile.WriteBool(xiaojie,guanjian,zhi2);
myinifile.Free;
end;
//当这个文件不存在时,上面语句还会自动创建ini文件
五,删除关键字
myinifile.deletekey('小节名','关键字');
六,删除小节
myinifile.erasesection('小节名');
七,小节操作
myinifile.readsection('小节名',tstring 变量); //可将指定小节中所有关键字名读取至一个字符串列表变量中;
myinifile.readsections(tstring 变量); //可将ini文件中所有小节名读取至一个字符串列表变量中去;
myinifile.readsectionvalues('小节名',tstring 变量); //可将ini文件中指定小节所有行(包括关键字,=,值)读取至一个字符串变量中去;
八,释放
myinifile.destory;