首页 > 代码库 > Delphi INI文件保存与读取

Delphi INI文件保存与读取

//需要引用IniFiles
uses
  system.IniFiles;
//保存INI配置文件
procedure TForm1.btnSaveClick(Sender: TObject);
var
  myIniFile: TIniFile;
  filepath: string;
begin
  filepath := ExtractFilePath(Application.Exename) + DBConfig.ini; //取得ini文件的路径
  myIniFile := TIniFile.Create(filepath);
  try
    myIniFile.WriteString(DBConf, Server, edtServer.Text);     //服务器地址
    myIniFile.WriteString(DBConf, Port, edtPort.Text);         //数据库端口
    myIniFile.WriteString(DBConf, Database, edtDatabase.Text); //数据库名
    myIniFile.WriteString(DBConf, User, edtUser.Text);         //登录用户名
    myIniFile.WriteString(DBConf, Password, edtPwd.Text);      //登录密码
    ShowMessage(保存成功);
  finally
    myIniFile.Free;
  end;
end;
//载入INI配置文件
procedure TForm1.btnLoadClick(Sender: TObject);
var
  myIniFile: TIniFile;
  filepath: string;
begin
  filepath := ExtractFilePath(Application.Exename) + DBConfig.ini; //取得ini文件的路径
  myIniFile := TIniFile.Create(filepath);
  try
    ListBox1.Clear;
    ListBox1.Items.Add(服务器: + myIniFile.ReadString(DBConf, Server, ‘‘));     //服务器地址
    ListBox1.Items.Add(端口: + myIniFile.ReadString(DBConf, Port, ‘‘));         //数据库端口
    ListBox1.Items.Add(数据库: + myIniFile.ReadString(DBConf, Database, ‘‘));   //数据库名
    ListBox1.Items.Add(用户名: + myIniFile.ReadString(DBConf, User, ‘‘));       //登录用户名
    ListBox1.Items.Add(密码: + myIniFile.ReadString(DBConf, Password, ‘‘));     //登录密码
  finally
    myIniFile.Free;
  end;
end;

界面效果

技术分享

Delphi INI文件保存与读取