

Ini文件的读写
一、Ini文件的基本操作步骤,引用类--声明--初始化--读/写
uses IniFiles
声明,全局使用放在public中,某处使用则在过程的var中
public
Testini:Tinifile;
Ininame:string;
初始化
Ininame := ExtractFilePath(ParamStr(0)) + 'Test.ini'; //*开始把加的部分括到前面括号里了,这里要有完整的路径,即便是当前目录也要
TestIni := TIniFile.Create(Ininame); //初始化
对文件读与写
try
if FileExists(Ininame) then
begin
Application.ProcessMessages; //这里表响应的进程仍可处理其它事件!
Edit1.Text := TestIni.ReadString('Test','test1','12345'); //ReadString三个参数,第一表那一部分,第二表读取的参数名,第三表没读到显示默认值
TestIni.WriteString('Test','Test1',Edit1.Text); //写入方法
end;
except
TestIni.Free; //释放
end;
//TestIni.Destroy; //销毁
IniFile.Free与IniFile.Destroy区别,推荐使用.free,
功能差不多,都是销毁对象释放资源。.free会先检查对象是否为nil,对象不为nil时才会调用.Destroy销毁; .Destroy不会检查对象是否为nil,如对象为nil时,用.Destroy会出错。只有你确定对象不为nil时才能用destory
二、Ini文件函数式操作方法
1. 函数式写入
//往Ini文件写入值的函数,该函数如Ini文件存在则直接写入,不存在则自动创建再写入
procedure SetIniFile(IniPath, Section, Key, val: string); //分别为Ini文件路径,节名,键名,键值
var
iniFile: TIniFile;
begin
iniFile := TIniFile.Create(IniPath);
try
iniFile.WriteString(Section, Key, val);
finally
iniFile.Free;
end;
end;
//调用
SetIniFile(ExtractFilePath(ParamStr(0))+'rs3.ini','Section1','aaa','bbb');//或者用GetEnvironmentVariable('Appdata') + '\softname\'
2. 函数式读取, 从ini文件读取指定的函数
function ReadIniFile1(const IniPath, Section, Key, defaultVal: string): string; //分别为Ini文件路径,节名,键名,没读取到值时的缺省值
var
iniFile: TIniFile;
begin
Result := '';
iniFile := TIniFile.Create(IniPath);
try
Result := iniFile.ReadString(Section, Key, defaultVal);
finally
iniFile.Free;
end;
end;
//调用
ReadIniFile1(ExtractFilePath(ParamStr(0))+'rs3.ini','Section1','aaa','1111');//或者用GetEnvironmentVariable('Appdata') + '\softname\'
======================================
三、 一些函数
//获取Ini文件中的所有节列表并输出
function getSectionsList(const iniPath: string): TStringList;
var
IniFile: TIniFile;
SectionList: TStringList;
begin
IniFile := TIniFile.Create(iniPath);
SectionList := TStringList.Create;
Result := TStringList.Create; // 创建 Result 用于返回
try
// 获取所有节名
IniFile.ReadSections(SectionList);
// 如果有节名,将它们赋值给 Result
if SectionList.Count > 0 then
Result.Assign(SectionList); // 使用 Assign 复制内容
finally
SectionList.Free; // 释放 SectionList
IniFile.Free; // 释放 IniFile
end;
end;
//输出所有节列表
procedure TForm1.Button2Click(Sender: TObject);
var
SectionList: TStringList;
i: Integer;
begin
SectionList := getSectionsList(ExtractFilePath(ParamStr(0)) +'rs.ini');
try
for i := 0 to SectionList.Count - 1 do
Memo1.Lines.Add(SectionList[i]);
finally
SectionList.Free;
end;
end;
//将ListView中的数据保存到ini
procedure SaveListViewToFile(ListView: TListView; const FileName: string);
var
IniFile: TIniFile;
i, j: Integer;
SectionName: string;
begin
IniFile := TIniFile.Create(FileName);
try
for i := 0 to ListView.Items.Count - 1 do
begin
SectionName := 'Item' + IntToStr(i);
IniFile.WriteString(SectionName, 'Text', ListView.Items[i].Caption);
// 保存子项
for j := 0 to ListView.Items[i].SubItems.Count - 1 do
begin
IniFile.WriteString(SectionName, 'SubItem' + IntToStr(j), ListView.Items[i].SubItems[j]);
end;
end;
finally
IniFile.Free;
end;
end;
//将ini数据读取的Listview
procedure LoadListViewFromFile(ListView: TListView; const FileName: string);
var
IniFile: TIniFile;
i, j: Integer;
SectionName: string;
ItemText: string;
SubItemText: string;
begin
IniFile := TIniFile.Create(FileName);
try
i := 0;
while IniFile.SectionExists('Item' + IntToStr(i)) do
begin
SectionName := 'Item' + IntToStr(i);
ItemText := IniFile.ReadString(SectionName, 'Text', '');
with ListView.Items.Add do
begin
Caption := ItemText;
// 加载子项
j := 0;
while IniFile.ValueExists(SectionName, 'SubItem' + IntToStr(j)) do
begin
SubItemText := IniFile.ReadString(SectionName, 'SubItem' + IntToStr(j), '');
SubItems.Add(SubItemText);
Inc(j);
end;
end;
Inc(i);
end;
finally
IniFile.Free;
end;
end;