Working with INI Files in Delphi - Working with Sections
(Page 3 of 4 )
The TIniFile class also provides methods to read and erase entire sections of the INI file at one go. The ReadSections method can be used to read all sections of an INI file to a string list, but if you want to read all the keys of the section you need to call the ReadSection; routine and pass the Section name as the first parameter. To read all the keys and their values in a specific section you can use the method named ReadSectionValues. This method, like the ReadSection method, requires you to pass the name of the section as the first parameter and specify a TStringList object as the second parameter. Below are examples of all four methods. I have added four buttons to the form and named them btnReadSections, btnReadSection, btnReadSectionValues, btnEraseSection. I am including the corresponding routines within the event handlers of the buttons.
procedure Tform1.btnReadSections(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.ReadSections(Memo1.Lines);
finally
myIniFile.Free;
end;
end;
procedure Tform1.btnReadSection(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.ReadSection('CompanyInfo',Memo1.Lines);
finally
myIniFile.Free;
end;
end;
procedure Tform1.btnReadSectionValues(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.ReadSectionValues('CompanyInfo',Memo1.Lines);
finally
myIniFile.Free;
end;
end;
To delete an entire section call the EraseSection method.
procedure Tform1.btnEraseSection(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.EraseSection('CompanyInfo');
finally
myIniFile.Free;
end;
end;
Now that we have examined most of the Read and Write methods of the TIniFile class, let us implement the class in a real world application. The Delphi 7 help file provides a code snippet that stores information pertaining to the position of the window in relation to the desktop in the INI file and uses it to display the window in the same position when the application is started again. I am providing a modified version of the code that reads from and writes desktop coordinates to the INI file. It is obvious that, since we intend to display the window in the same position as it was last time, we should read the values from the INI file on the form's OnCreate event and write the values to the INI file in the form's OnClose event handler.
procedure TForm1.FormCreate(Sender: TObject);
var
myIniFile: TIniFile;
begin
myIniFile := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
try
Top := myIniFile .ReadInteger( 'Form', 'Top', 100 );
Left := myIniFile.ReadInteger( 'Form', 'Left', 100 );
Caption := myIniFile.ReadString( 'Form', 'Caption', 'Form1' );
if myIniFile.ReadBool( 'Form', 'InitMax', false ) then
WindowState = wsMaximized
else
WindowState = wsNormal;
finally
myIniFile.Free;
end;
end;
Note:The code after the TINIFile's Constructor call is put in guarded try/finally blocks to prevent memory leaks.
In the above code we are instantiating a TIniFile object, assigning the applications INI file to it and changing the Top, Left, Caption and WindowState properties of Form1 with the values retrieved from the INI file. The third parameter of the two ReadInteger methods contain values of 100 so that if the INI file does not contain a value for these properties, these values would be substituted and Form1's Top and Left properties should have values of 100 each.
Now we come to the writing part. Since we intend to save the position of the form when the application terminates, the OnClose event handler of Form1 would be the perfect place to include the code that writes values to the INI file.
procedure TForm1.FormClose(Sender: TObject; var Action TCloseAction)
var
myIniFile: TIniFile;
begin
myIniFile := TIniFile.Create( ChangeFileExt( Application.ExeName, '.INI' ) );
try
myIniFile.WriteInteger( 'Form', 'Top', Top);
myIniFile.WriteInteger( 'Form', 'Left', Left);
myIniFile.WriteString( 'Form', 'Caption', Caption );
myIniFile.WriteBool( 'Form', 'InitMax', WindowState = wsMaximized );
finally
myIniFile.Free;
end;
end;
Next: TRegistryIniFile Class >>
More Delphi-Kylix Articles
More By Danish Ahmed