Beranda > Delphi > Reading and writing INI files

Reading and writing INI files

Here is an overloaded group of commands to read from or write to an INI file. The routines are overloaded based on the d parameter. s is the [GROUP], k is Left side (key), d is the default or new parameter and f is the filename to use.

function GetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteBool(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadBool(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d: boolean; f: string=''): boolean; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteBool(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadBool(s,k,d);
  FreeAndNil(ini);
end;

function GetIni(s,k: string; d: integer; f: string=''): integer; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteInteger(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadInteger(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d:integer; f: string=''): integer; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteInteger(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadInteger(s,k,d);
  FreeAndNil(ini);
end;

function GetIni(s,k: string; d: string; f: string=''): string; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  if ini.ReadString(s,k,'') = '' then
  begin
    ini.WriteString(s,k,d);
    ini.UpdateFile;
  end;
  Result := ini.ReadString(s,k,d);
  FreeAndNil(ini);
end;

function SetIni(s,k: string; d: string; f: string=''): string; overload;
var
  ini: TMemIniFile;
begin
  Result := d;
  if f = '' then
  begin
    ini := TMemIniFile.Create(lowercase(ChangeFileExt(ParamStr(0),'.ini')));
  end
  else
  begin
    if not FileExists(f) then Exit;
    ini := TMemIniFile.Create(f);
  end;
  ini.WriteString(s,k,d);
  ini.UpdateFile;
  Result := ini.ReadString(s,k,d);
  FreeAndNil(ini);
end;
Iklan
Kategori:Delphi
  1. September 21, 2014 pukul 3:03 am

    I always spent my half an hour to read this web site’s content everyday along with a mug of coffee.

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: