Skip to main content

Cara Menampilkan Daftar Section Dan Key File INI

cara menampilkan daftar section dan key file INI di delphi
Sebuah file INI dibangun oleh sekumpulan section, setiap section terdiri atas key-key, yang masing-masing key memiliki value.

Untuk menampilkan semua nama section pada sebuah file INI, Anda dapat menggunakan fungsi ReadSections(Strings: TStrings).

Parameter strings menunjuk pada objek TString. Objek ini bisa mengacu pada properti sebuah komponen, misalnya Items pada komponen TListBox, ataupun menunjuk objek TStrings sendiri.

Menampilkan nama-nama key dari sebuah section


Untuk menampilkan nama key, gunakan fungsi ReadSection(const Section: Strings; Strings: TStrings).
Parameter section menyatakan nama section yang berisi key yang ingin ditampilkan. Parameter strings menunjuk pada objek TStrings.

Menampilkan nama-nama key beserta value dari sebuah section


Fungsi ReadSectionValues(const Section: String; Strings: TStrings) mirip dengan fungsi ReadSection, namun hasil pembacaan juga meliputi value. Nama Section yang ditampilkan hanya section yang mempunyai nilai.

Listing program berikut ini berguna untuk menampilkan daftar section, key, serta value dari file "Win.ini".

Perlu diketahui, ketika Anda bekerja dengan file INI sistem (misalnya Win.ini, System.ini), secara otomatis Delphi juga melakukan pembacaan pada registry. Jadi, nilai-nilai output program berikut kemungkinan tidak seluruhnya berlokasi pada file "Win.ini".

Pada bagian deklarasi Private, definisikan varibel sebagai berikut,

AppINI: TIniFile;
SelSection, SelKey: String;

Listing kode program selengkapnya adalah sebagai berikut:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    ListBox2: TListBox;
    GroupBox1: TGroupBox;
    ListBox3: TListBox;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Label5: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1Click(Sender: TObject);
    procedure ListBox2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    AppINI: TIniFile;
    SelSection, SelKey: String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label5.Caption:= 'Daftar Section dan Key yang ditampilkan' + #13 +
    'pada ListBox di samping kemungkinan ' + #13 +
    'tidak seluruhnya ada pada file "Win.ini",' + #13 +
    'melainkan berlokasi pada registry ' + #13 +
    'Windows.';
  AppINI:= TIniFile.Create('Win.ini');
  AppINI.ReadSections(ListBox1.Items);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  AppINI.Free;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  SelSection:= ListBox1.Items[ListBox1.ItemIndex];
  Label2.Caption:= 'Daftar Key section "' + SelSection + '" :';
  Label3.Caption:= 'Daftar Key beserta Value section "' + SelSection + '" :';
  ListBox2.Items.Clear;
  ListBox3.Items.Clear;
  AppINI.ReadSection(ListBox1.Items[ListBox1.ItemIndex], ListBox2.Items);
  AppINI.ReadSectionValues(ListBox1.Items[ListBox1.ItemIndex], ListBox3.Items);
end;

procedure TForm1.ListBox2Click(Sender: TObject);
begin
  SelKey:= ListBox2.Items[ListBox2.ItemIndex];
  Label4.Caption:= 'Value dari Key "' + SelKey + '" :';
  Edit1.Text:= AppINI.ReadString(SelSection, SelKey, '');
end;

end.

Ketika di jalankan maka hasilnya akan seperti gambar berikut. Sampai bertemu pada artikel selanjutnya. Terimakasih atas kunjungannya dan.. Salam.

cara menampilkan daftar section dan key file INI di delphi

Comments