Delphi TStrings取得Ini文件键值对

 

Delphi TStrings取得Ini文件键值对的简单方法如下:

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文件如下:

[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

 

 

 

 

你可能感兴趣的:(String)