Javen的文档


INI 文件的基本读写操作

<p>使用步骤: 1.uses中加入IniFiles。 2.创建窗体是设置配置文件名路径和加载配置文件。 设置配置文件名路径:ConfigFile := ExtractFilePath(Application.ExeName) + &#039;config.ini&#039;; 加载配置: 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) + &amp;#039;config.ini&amp;#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(&amp;#039;Database&amp;#039;, &amp;#039;Host&amp;#039;, &amp;#039;localhost&amp;#039;); Edit2.Text := Ini.ReadString(&amp;#039;Database&amp;#039;, &amp;#039;Username&amp;#039;, &amp;#039;admin&amp;#039;); // 读取布尔值 CheckBox1.Checked := Ini.ReadBool(&amp;#039;Settings&amp;#039;, &amp;#039;AutoSave&amp;#039;, True); // 读取整数和组合框 ComboBox1.ItemIndex := Ini.ReadInteger(&amp;#039;Settings&amp;#039;, &amp;#039;Theme&amp;#039;, 0); // 读取其他类型的数据 // Width := Ini.ReadInteger(&amp;#039;Window&amp;#039;, &amp;#039;Width&amp;#039;, 600); // Height := Ini.ReadInteger(&amp;#039;Window&amp;#039;, &amp;#039;Height&amp;#039;, 400); finally Ini.Free; end; end; procedure TForm1.SaveConfig; var Ini: TIniFile; begin Ini := TIniFile.Create(ConfigFile); try // 写入字符串值 Ini.WriteString(&amp;#039;Database&amp;#039;, &amp;#039;Host&amp;#039;, Edit1.Text); Ini.WriteString(&amp;#039;Database&amp;#039;, &amp;#039;Username&amp;#039;, Edit2.Text); // 写入布尔值 Ini.WriteBool(&amp;#039;Settings&amp;#039;, &amp;#039;AutoSave&amp;#039;, CheckBox1.Checked); // 写入整数 Ini.WriteInteger(&amp;#039;Settings&amp;#039;, &amp;#039;Theme&amp;#039;, ComboBox1.ItemIndex); // 写入其他数据 // Ini.WriteInteger(&amp;#039;Window&amp;#039;, &amp;#039;Width&amp;#039;, Width); // Ini.WriteInteger(&amp;#039;Window&amp;#039;, &amp;#039;Height&amp;#039;, Height); ShowMessage(&amp;#039;配置已保存到: &amp;#039; + ConfigFile); finally Ini.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin // 手动保存配置 SaveConfig; end; procedure TForm1.Button2Click(Sender: TObject); begin // 重新加载配置 LoadConfig; ShowMessage(&amp;#039;配置已重新加载&amp;#039;); end; end.</code></pre>

页面列表

ITEM_HTML