INI 文件的基本读写操作
<p>使用步骤:
1.uses中加入IniFiles。
2.创建窗体是设置配置文件名路径和加载配置文件。
设置配置文件名路径:ConfigFile := ExtractFilePath(Application.ExeName) + 'config.ini';
加载配置: LoadConfig;
3.关闭窗体是保存配置文件。
SaveConfig;
4.创建读取和保存过程,读取和保存写在过程中。
5.读取和保存配置。</p>
<h3>主要方法和属性说明</h3>
<p>TIniFile.Create() - 创建 INI 文件对象</p>
<p>ReadString() - 读取字符串值</p>
<p>WriteString() - 写入字符串值</p>
<p>ReadInteger() - 读取整数值</p>
<p>WriteInteger() - 写入整数值</p>
<p>ReadBool() - 读取布尔值</p>
<p>WriteBool() - 写入布尔值</p>
<p>ReadSections() - 读取所有节名</p>
<p>ReadSection() - 读取某个节的所有键名</p>
<p>EraseSection() - 删除整个节</p>
<p>DeleteKey() - 删除某个键</p>
<p>完整单元示例:</p>
<pre><code class="language-delphi">unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls, IniFiles;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
CheckBox1: TCheckBox;
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ private declarations }
ConfigFile: string;
procedure LoadConfig;
procedure SaveConfig;
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// 设置配置文件路径
ConfigFile := ExtractFilePath(Application.ExeName) + &#039;config.ini&#039;;
// 加载配置
LoadConfig;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
// 窗体关闭时自动保存配置
SaveConfig;
end;
procedure TForm1.LoadConfig;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(ConfigFile);
try
// 读取字符串值
Edit1.Text := Ini.ReadString(&#039;Database&#039;, &#039;Host&#039;, &#039;localhost&#039;);
Edit2.Text := Ini.ReadString(&#039;Database&#039;, &#039;Username&#039;, &#039;admin&#039;);
// 读取布尔值
CheckBox1.Checked := Ini.ReadBool(&#039;Settings&#039;, &#039;AutoSave&#039;, True);
// 读取整数和组合框
ComboBox1.ItemIndex := Ini.ReadInteger(&#039;Settings&#039;, &#039;Theme&#039;, 0);
// 读取其他类型的数据
// Width := Ini.ReadInteger(&#039;Window&#039;, &#039;Width&#039;, 600);
// Height := Ini.ReadInteger(&#039;Window&#039;, &#039;Height&#039;, 400);
finally
Ini.Free;
end;
end;
procedure TForm1.SaveConfig;
var
Ini: TIniFile;
begin
Ini := TIniFile.Create(ConfigFile);
try
// 写入字符串值
Ini.WriteString(&#039;Database&#039;, &#039;Host&#039;, Edit1.Text);
Ini.WriteString(&#039;Database&#039;, &#039;Username&#039;, Edit2.Text);
// 写入布尔值
Ini.WriteBool(&#039;Settings&#039;, &#039;AutoSave&#039;, CheckBox1.Checked);
// 写入整数
Ini.WriteInteger(&#039;Settings&#039;, &#039;Theme&#039;, ComboBox1.ItemIndex);
// 写入其他数据
// Ini.WriteInteger(&#039;Window&#039;, &#039;Width&#039;, Width);
// Ini.WriteInteger(&#039;Window&#039;, &#039;Height&#039;, Height);
ShowMessage(&#039;配置已保存到: &#039; + ConfigFile);
finally
Ini.Free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
// 手动保存配置
SaveConfig;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// 重新加载配置
LoadConfig;
ShowMessage(&#039;配置已重新加载&#039;);
end;
end.</code></pre>