请教如何可以方便的获取ini文件的所有键值
1个回答
展开全部
Delphi TStrings取得Ini文件键值对的简单方法如下:
[delphi] view plain copy
procedure TForm1.BitBtn1Click(Sender: TObject);
var
StrList:TStrings;
I:Integer;
IniCfg:TInifile;
begin
Memo1.Clear;
StrList := TStringList.Create;//TStrings中含有抽象方法,是抽象类,不能实例化
IniCfg := TInifile.Create( ExtractFilePath(Application.ExeName)+'SysCfg.ini');
try
IniCfg.ReadSectionValues('SubTitle',StrList);
for I := 0 to StrList.Count - 1 do
begin
Memo1.Lines.Add('Key:'+StrList.Names[i]);
Memo1.Lines.Add('Value:'+StrList.ValueFromIndex[i]);
// Memo1.Lines.Add('Value:'+StrList.Values[StrList.Names[i]]);//也可以
end;
finally
IniCfg.Free;
StrList.Free;
end;
end;
SysCfg.ini文件如下:
[plain] view plain copy
[SubTitle]
SubTitle1=子标题1
SubTitle2=子标题2
SubTitle3=子标题3
SubTitle4=子标题4
SubTitle5=子标题5
输出结果如下:
Key:SubTitle1
Value:子标题1
Key:SubTitle2
Value:子标题2
Key:SubTitle3
Value:子标题3
Key:SubTitle4
Value:子标题4
Key:SubTitle5
Value:子标题5
[delphi] view plain copy
procedure TForm1.BitBtn1Click(Sender: TObject);
var
StrList:TStrings;
I:Integer;
IniCfg:TInifile;
begin
Memo1.Clear;
StrList := TStringList.Create;//TStrings中含有抽象方法,是抽象类,不能实例化
IniCfg := TInifile.Create( ExtractFilePath(Application.ExeName)+'SysCfg.ini');
try
IniCfg.ReadSectionValues('SubTitle',StrList);
for I := 0 to StrList.Count - 1 do
begin
Memo1.Lines.Add('Key:'+StrList.Names[i]);
Memo1.Lines.Add('Value:'+StrList.ValueFromIndex[i]);
// Memo1.Lines.Add('Value:'+StrList.Values[StrList.Names[i]]);//也可以
end;
finally
IniCfg.Free;
StrList.Free;
end;
end;
SysCfg.ini文件如下:
[plain] view plain copy
[SubTitle]
SubTitle1=子标题1
SubTitle2=子标题2
SubTitle3=子标题3
SubTitle4=子标题4
SubTitle5=子标题5
输出结果如下:
Key:SubTitle1
Value:子标题1
Key:SubTitle2
Value:子标题2
Key:SubTitle3
Value:子标题3
Key:SubTitle4
Value:子标题4
Key:SubTitle5
Value:子标题5
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询