Working with INI Files in Delphi - TIniFile and TMemIniFile Classes
(Page 2 of 4 )
Delphi VCL provides TIniFile and TMemIniFile classes to allow programmers to write and read data to an INI file from within their programs. The TMemIniFile class, unlike TIniFile classes, has no 64 KB size limit, but it does not write to the disk; it writes to a memory buffer and saves to disk only after the "updatefile" method has been called. To use the TIniFile class you need to include inifiles.pas unit file in the "uses" clause of your code.
To instantiate a TIniFile class object you would be required to pass the name (which happens to be the only property of this class) of the INI file as the parameter of the constructor. If the file doesn't exist it will be created.
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create('Test.ini');
end;
In the above instance the INI file is created and saved in the Windows folder since we have not specified a path, but it can be saved in any location as long as you provide a valid path along with the file name. However, it would be a better idea to save the file in the application folder and assign it the same name as the application itself. This is made easy by the ChangeFileExt routine which can be found in the SysUtils.pas unit file. This function takes the filename as the first parameter and changes its extension to the extension specified with the second parameter. Thus the above code can be now written as:
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
end;
The INI file created will have the same filename as the executable and will be saved in the same location as well.
Write and Read Methods
The TIniFile class provides different "Write" and "Read" methods which you can use depending upon whether you are handling values or sections, and in case of the former, the data type you are handling. To write a string value to a .INI file you can call the WriteString method, but if the value is an integer you will need to call the WriteInteger method. In the same manner, you can call WriteDate, WriteBool, WriteDateTime, WriteTime, WriteFloat and WriteBinaryStream depending on the data type of the value.
WriteString(const Section: string; const Ident: string; const Value: string);
Each Write method requires three parameters. The first parameter identifies the section of the file. The second parameter,"Ident," identifies the key you want to write to and the third parameter contains the value that should be written with the key.
uses inifiles.pas;
...
procedure Tform1.btnWriteClick(Sender:TObject);
var
myIniFile:TIniFile;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
myIniFile.WriteString('CompanyInfo','CompanyName','mindfire');
myIniFile.WriteInteger('CompanyInfo',' IntVal',100);
finally
myIniFile.Free;
end;
end;
The code above creates an INI file with the same name as the application. For example, if your project name is TestProject1, the TestProject1.ini file is created in the output folder. Open the INI file in notepad; you should be able to see the following:
[CompanyInfo]
CompanyName=mindfire
IntVal=100
Like Write methods, the TIniFile class also provides Read methods to read data from an INI file. The Read methods are identical to the Write methods in every way except that the data is read from the INI file and not written.
ReadString(const Section:string; const Ident: string; const Value: string);
The ReadString method also requires three parameters. The first parameter corresponds to the Section name, the second to the Key name and the third parameter contains the default value to be read in case the key does not contain any values. In the following example we will read from the INI file we created earlier and display the value in a message box. If the key does not contain any value, the text "No Value" will be displayed instead.
procedure Tform1.btnReadClick(Sender:TObject);
var
myIniFile:TIniFile;
sVal:string;
iVal:integer;
begin
myIniFile:=TIniFile.Create(ChangeFileExt( Application.Exename,'.ini'));
try
sVal:= myIniFile.ReadString('CompanyInfo,'CompanyName','No Value');
iVal:=myIniFile.ReadInteger('CompanyInfo,'IntVal',0);
ShowMessage(sVal + IntToStr(iVal));
finally
myIniFile.Free;
end;
end;
Next: Working with Sections >>
More Delphi-Kylix Articles
More By Danish Ahmed