procedure TForm1.Button1Click(Sender: TObject);//点击按钮
begin
login(Edit1.Text,Edit2.Text); //传入两个编辑框中输入的值
end;
procedure TForm1.login(account,password:String);
var
ini:TIniFile; //定义一个ini文件,记得要在头文件中加入Inifiles
ac,pw:String;
begin
ini:= TInifile.Create('C:\Users\apple\Desktop\1.ini'); //若没有找到文件,则自动创建一个
ac:=ini.ReadString('login','account','');
//节点为login,account为关键字,‘’为[缺省值],该ini文件不存在该关键字时则将缺省值赋给ac
pw:=ini.ReadString('login','password','');
if (''.Equals(ac)) or (''.Equals(pw))then //判断ac或pw是否为空
begin
ini.EraseSection('login'); //删除节点
ini.WriteString('login','account','123'); //写入ini文件
ini.WriteString('login','password','123456');
end;
if ac.Equals(account) and pw.Equals(password) then
begin
Label3.Visible:=TRUE;
Label3.Caption:='输入正确';
end //记得else前的end后无分号
else
begin
Label3.Visible:=TRUE;
Label3.Caption:='输入错误';
end;
end;
界面:
创建的1.ini文件:
总结:
;用引号开头写INI文件的注释
[节点]
关键字=值
读取关键字的值:
ini.Readstring('节点','关键字',缺省值); // string类型,还有Integer、bool的
写入ini文件:
ini.Writestring('节点','关键字',变量或字符串值);