Write/copy ini section to registry


I can save some configuration settings for my app to an ini file for loading when the app starts up. I would like to be able to save those same settings to the registry, optionally loaded from the ini files. Can someone explain to me how to load the ini/text file to the registry using name/value pairs? The ini structure is something like the following:

Essentialy, I have some listviews which are loaded from text/ini files that I want to be able to 'backup' to the registry. Or at least have another option, distinct from ini, for saving the data. I have just heard about TRegIniFile, and it sounds like it ought to be able to do both, but I don't understand how to use it to write to the registry.


Better use Delphi's TRegistryIniFile instead of TRegIniFile, it has more possibilities and it's more compatible with TIniFile.

Also, if you switch from INI files to the registry, you can rewrite your application with a minimum of coding changes: only change the code for Create.
TRegistryIniFile interprets the FileName property as a subkey under the registry's root key, HKEY_CURRENT_USER by default. So, instead of:

IniFile := TIniFile.Create('C:\Software\MyApp');

you code it as:

RegistryIniFile := TRegistryIniFile.Create('Software\MyApp');

Most methods are the same as in TIniFile: ReadString, ReadInteger, ReadFloat,..., WriteString, WriteInteger, WriteFloat,..., ReadSection, ReadSections, DeleteKey, EraseSection, and so on... Don't worry about what happens behind the scenes: sections are treated as sub-keys in the registry, and data entries under a section are treated as ident/data value pairs of that sub-key.
For example, the following code writes an ident/value pair in the key HKEY_CURRENT_USER\Software\MyApp\Images:

  RIniFile: TRegistryIniFile;
  RIniFile := TRegistryIniFile.Create('Software\MyApp');
  RIniFile.WriteString('Images', C:\Pics\Latest\bird.tif',
                       'Original image as shot from camera');

The key is created automatically if it doesn't exist already, otherwise you overwrite the existing value(s).

Members of the DelphiLand Club can access the full version of our tutorials, including all fully commented source code files.

Click here to become a DelphiLand Club member.