The program can store its settings in the registry or ini-files. Or it can store its settings in both the registry and ini-files. Storing such configuration data in the registry allows each computer user to have their own settings, as well as to save the settings for an application that runs in conditions where there is no permission to directly change the contents of the (disk) media. For example, it can be a compact disc, or a media with a blocking of the possibility of changing information. Storing settings in an ini-file has its advantages. For example, it allows all computer users to have the same settings that are not lost when reinstalling the operating system. In addition, the use of ini files by applications slightly "lightens" the size of the operating system registry.
Conclusion from the above: it is sometimes necessary to work with ini-files when developing applications. And there is such an object in KOL.
Its constructor:
OpenIniFile(s)- creates an object of type TIniFile, returning a pointer to it of type PIniFile. The newly created object is either linked to an existing ini-file (named s), or if such a file does not exist at the time of the call, it is created automatically.
This KOL object for working with ini-files has a feature that allows in some cases to use the same procedure in order to ensure both loading and saving of settings. Both reading and writing of key values are performed by the same methods of the object. What exactly to do, read or write, determines the mode of work with the settings file (the Mode property).
Properties and methods of the TIniFile object:
Mode - operating mode: ifmRead - read, ifmWrite - write;
Filename - the name of the settings file (read-only);
Section - section of the ini-file (in the settings file, the section begins with a line containing the section name in square brackets);
ValueInteger(key, i)- in read mode, it returns the value of the key, while the value i is used as the default value, which is returned if there is no key in the current section; in write mode, the same method writes a new value i for key;
ValueString(key, s) - similar to the previous method, but for the string value of the key;
ValueBoolean(key, s) - the same for a boolean value;
ValueData(key, buf, i) - similar to the previous methods, but the work is carried out with a data block of length i bytes;
ValueDouble(key, d) - the same for a real number of type Double;
ClearAll - complete cleaning of the settings file (all sections are deleted together with all keys);
ClearKey(s) - removes the key s in the current section;
GetSectionNames(SL) - reads into the object of the list of lines (PStrList) the names of all sections from the settings file;
GetSectionData(SL) - reads the entire contents of the current section into the object of the list of strings.
I will give a small example of how it is possible with the same code to ensure both recording and saving of settings. Suppose, for example, we want to save the coordinates of the application window in the settings file at the end of the work, and restore them from there at the beginning of work. Let's create a method that will perform both of these operations:
procedure MyObj.ReadWriteIni (write: boolean);
var ini: PIniFile;
begin
ini: = OpenIniFile (GetStartDir + 'my.ini');
if write then ini.Mode: = ifmWrite;
ini.Section = 'position';
form.Left: = ini.ValueInteger ('Left', form.Left);
form.Top: = ini.ValueInteger ('Top', form.Top);
ini.Free;
end;
Now, when starting the application, we will organize a call to this method with the false parameter, and when closing - with the true parameter. I leave it to you to figure out why this code will do everything that is required of it, although the same operators work in both cases.