读写ini文件实例

procedure TForm1.FormCreate(Sender: TObject);
var
  IniFile: TIniFile;
begin
  IniFile:=TIniFile.Create(ExtractFilePath(paramstr(0))+'Test.ini');
  try
    if not FileExists(ExtractFilePath(paramstr(0))+'Test.ini') then
    begin
      IniFile.WriteInteger('Form','Top',self.Top);
      IniFile.WriteInteger('Form','Left',self.Left);
      IniFile.WriteInteger('Form','Height',self.Height);
      IniFile.WriteInteger('Form','Width',self.Width);
    end
    else
    begin
        self.Top:=IniFile.ReadInteger('Form','Top',0);
        self.Left:=IniFile.ReadInteger('Form','Left',0);
        self.Height:=IniFile.ReadInteger('Form','Height',Screen.Height);
        self.Width:=IniFile.ReadInteger('Form','Width',Screen.Width);
    end;
  finally
    IniFile.Free;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  IniFile: TIniFile;
begin
  IniFile:=TIniFile.Create(ExtractFilePath(paramstr(0))+'Test.ini');
  IniFile.WriteInteger('Form','Top',self.Top);
  IniFile.WriteInteger('Form','Left',self.Left);
  IniFile.WriteInteger('Form','Height',self.Height);
  IniFile.WriteInteger('Form','Width',self.Width);
  IniFile.Free;
  Action:=CaFree;
end;

你可能感兴趣的:(ini)