delphi INI 文件

可以参考以下代码段
procedure TForm1.FormCreate(Sender: TObject);
begin
// 使用TIniFile类的Create成员函数建立TIniFile对
//象,该对象用来读写当前目录中的sample.ini文件,
  IniFile := TIniFile.Create('..\sample.ini');
//读取节点
  cobSection.Clear;
  IniFile.ReadSections(cobSection.Items);
  cobSection.ItemIndex := 0;
  cobSectionChange(sender);
  btnSave.Enabled := false;

end;

procedure TForm1.cobSectionChange(Sender: TObject);
begin
  cobIni.Clear;
// 将cobSection中所选择节中对应的各个
//变量及对应的值送入cobIni
  IniFile.ReadSection(cobSection.Text, cobIni.Items);
  cobIni.ItemIndex := 0;
  cobIniChange(Sender);
end;

procedure TForm1.cobIniChange(Sender: TObject);
begin
  btnSave.Enabled := false;
//将选择的关键字值读入
  Edit1.Text := IniFile.ReadString(cobSection.Text, cobIni.Text, '');

end; 

赞同

0

| 评论

2011-8-2 14:45 HJ_3000 | 五级

Unit Unit1;

Interface

Uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IniFiles, StdCtrls, Buttons;

Type
  TForm1 = Class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    BitBtn1: TBitBtn;
    Procedure FormCreate(Sender: TObject);
    Procedure BitBtn1Click(Sender: TObject);
  Private
    { Private declarations }
  Public
    { Public declarations }
  End;

Var
  Form1: TForm1;

Implementation

{$R *.dfm}

//INI配置文件操作: uses IniFiles

Function INI_New(Var aFile: String): Boolean;
Var
  atext: TextFile;
Begin
  aFile := Format('%sconfig.ini', [ExtractFilePath(Paramstr(0))]);
  If Not FileExists(aFile) Then
  Begin
    AssignFile(atext, aFile);
    Rewrite(atext);
    CloseFile(atext);
  End;
  Result := FileExists(aFile);
End;

Function INI_Put_Str(aItem, aKey, aStr: String): Boolean; // 写INI-文本
Var
  aFile: String;
Begin
  Result := False;
  If (Length(aItem) = 0) Or (Length(aKey) = 0) Then
  Begin
    Exit;
  End;
  If INI_New(aFile) Then
  Begin
    With TIniFile.Create(aFile) Do
    Begin
      Try
        WriteString(aItem, aKey, aStr);
        Result := True;
      Finally
        Free;
      End;
    End;
  End;
End;

Function INI_Get_Str(aItem, aKey: String): String; // 读INI-文本
Var
  aFile: String;
Begin
  Result := '';
  If (Length(aItem) = 0) Or (Length(aKey) = 0) Then
  Begin
    Exit;
  End;
  If INI_New(aFile) Then
  Begin
    With TIniFile.Create(aFile) Do
    Begin
      Try
        Result := ReadString(aItem, aKey, '');
      Finally
        Free;
      End;
    End;
  End;
End;
//可扩展下,如:INI_Get_Int     INI_Get_Bool  ...
Procedure TForm1.FormCreate(Sender: TObject);
Begin
  Edit1.Text := INI_Get_Str('Set', 'Work11');
  Edit2.Text := INI_Get_Str('Set', 'Work12');
End;

Procedure TForm1.BitBtn1Click(Sender: TObject); //保存
Begin
  INI_Put_Str('Set', 'Work11', Edit1.Text);
  INI_Put_Str('Set', 'Work12', Edit2.Text);
End;

End. 

赞同

0

| 评论

2011-8-4 10:42 天朝理想 | 二级

var myinifile:Tinifile;
begin
myinifile:=TInifile.Create(ExtractFilePath(paramstr(0))+'setup.ini');
Edit1.text:=myinifile.ReadString('配置设置','Work11','');
Edit2.text:=myinifile.ReadString('配置设置','Work12',''); 

posted on 2011-11-13 10:55  舟山牙医  阅读(669)  评论(0编辑  收藏  举报

导航