Monday, 18 February 2013

How to Create and Use INI Files in Delphi XE2?

How to Create and Use INI Files in Delphi XE2?

Create an INI file (config.ini) which will have your application configuration settings like database connection settings, log settings, email settings etc. INI file is must for a big delphi application as you need to change various settings for application time to time. If you maintain INI file then you don't have to go in delphi code or database to change the settings everytime, you can just open INI file and make your changes.

Suppose you have created following INI file (config.ini)

ABC = 10
XYZ = 'Hello User'

Now, you have one integer value and one string value in ini. Lets create an delphi function to read this simple ini file.

You will have to use TIniFile delphi component from VCL. Declare object of this component.

IniValues : TIniFile;

procedure TMyForm.ReadINIFilesDelphi;
 ABCValue : Integer;
 XYZValue : String;
    IniValues := TIniFile.Create('config.ini');
    ABCValue := IniValues.ReadInteger('ConfigSettings','ABC',0);
    XYZValue := IniValues.ReadString('ConfigSettings','XYZ','');

    on E : Exception do
      ShowMessage('Error occured in function ReadINIFilesDelphi: ' + E.Message);

Above delphi procedure used ReadInteger and ReadString to read from INI files. It assigns 0 as default value to ABCValue and '' to XYZValue.

No comments:

Post a Comment