For read and write the INI File from Delphi, you can used special class which provided by Delphi , that is TIniFile.
Step for using the TIniFiles :
1. On Uses section declare the IniFiles.
2. On Var Section declare variable which type is TIniFiles
3. Make the object TiniFiles type with Create(FileName:string) methode. FileName Parameter declare the name of file which will open. For the default the directory pointed to \Windows directory. If you work in the different directory used the absolute path.
4. Do INI File operations like write and read value.
5. Destroy TIniFiles Object with Free Methode
For Excample Code you can see at bellow :
Uses
IniFiles;
Implementation
Procedure Tfor1.Formcreate(Sender: Tobject);
Begin
Label1.Caption := ‘Tekan tombol berikut dan pada direktori’ + #13 + ‘”’ + GetcurrentDir + ‘”’ + #13 + ‘buka file “Seting.ini” yang terbentuk.’;
End;
Procedure Tform1.Button1Click(sender: TObject);
Var
Myinifile : TIniFile;
Begin
//membuat file INI bernama “Seting.ini”
MyInifile := TInifile.Create(GetcurrentDir + ‘\Seting.ini’);
//Mencatat posisi Form1.Top
Myinifile.WriteInteger(‘Posisi’, ’Top’, Form1.Top);
//Mencatat posisi Form1.Left
Myinifile.WriteInteger(‘Posisi’, ‘Left’, Form1.Left);
Myinifile.Free;
End;
Procedure TForm1.Button2Click(Sender: TObject);
Var
INIFileName : TFileName;
OpenedINIFile : TIniFile;
Begin
//Menentukan path dan nama File INI yang di buka
INIFileName := ExtractFilePath(Application.Exename) + ‘Seting.ini’;
//Membuka File INI
OpenedINIFile := TIniFile.Create(IniFileName);
//Menentukan posisi Form1.Top sesuai dg data pada file INI
Form1.Top := OpenedINIFile.ReadInteger(‘Posisi’, ‘Top’, 0);
//Menentukan posisi Form1.Left sesuai dengan data pada file INI
Form1.Left := OpenedINIFile.ReadInteger(‘Posisi’, ‘Left’, 0);
OpenedINIFile.Free;
End;
End.
No comments:
Post a Comment