Copy Delphi TIniFile to TRegistryIniFile and v.v.

Posted by John, DelphiLand Team on August 18, 2008

In Reply to Use Windows' Registry as INI files in Delphi posted by John, DelphiLand Team on August 16, 2008

: ... during the next days I'll post some more example source code.
: John

So, if Delphi's TIniFile and TRegistryIniFile are so much alike, how to copy one kind of INI to the other? Here are the general steps:

  1. Create both the TIniFile and TRegistriIniFile objects.
  2. From the "source" object, get a list of sections.
  3. From each section, get a list of "idents-and-values".
  4. From each "ident-and-value" pair, get the ident part.
  5. Get the value that corresponds to each ident.
  6. Copy each ident and its corresponding value to the "destination" object.
  7. Free the objects that you created.

Delphi source code example for copying an INI-file to its equivalent in the registry:

procedure TForm1.CopyIniToRegistry(FileName, RegKey: string);
var
  IniFile: TIniFile;
  RIniFile: TRegistryIniFile;
  Sections, IdentValues: TStringList;
  Section, Ident, Value: string;
  i, j: integer;
begin
  IniFile  := TIniFile.Create(FileName);             // (1)
  { RegKey will be created/accessed, under the
    registry's root key, HKEY_CURRENT_USER by default }
  RIniFile := TRegistryIniFile.Create(RegKey);       // (1)
  Sections    := TStringList.Create;
  IdentValues := TStringList.Create;
  IniFile.ReadSections(Sections.Items);              // (2)
  for i := 0 to slSections.Count - 1 do begin           
    Section := Sections.Items[i];                    
    IniFile.ReadSectionValues(Section, IdentValues); // (3)
    for j := 0 to IdentValues.Count - 1 do begin
      Ident := IdentValues.Names[i];                 // (4)
      Value := IdentValues.Values[Ident];            // (5)
      RIniFile.WriteString(Section, Ident, Value);   // (6)
    end;
  end;
  Sections.Free;                                     // (7) 
  IdentValues.Free;                                  // (7)
  IniFile.Free;                                      // (7)
  RIniFile.Free;                                     // (7)
end;
Probably the code above can be written more compactly, but I wanted to show you all the details :)

Now for the reverse, from RegistryIniFile to an INI-file. That code is almost identical, we only swap the TIniFile and the TRegistryIniFile objects:

procedure TForm1.CopyRegistryToIni(FileName, RegKey: string);
var
  IniFile: TIniFile;
  RIniFile: TRegistryIniFile;
  Sections, IdentValues: TStringList;
  Section, Ident, Value: string;
  i, j: integer;
begin
  IniFile  := TIniFile.Create(FileName);         
  RIniFile := TRegistryIniFile.Create(RegKey); 
  Sections    := TStringList.Create;
  IdentValues := TStringList.Create;
  RIniFile.ReadSections(Sections.Items);       
  for i := 0 to slSections.Count - 1 do begin           
    Section := Sections.Items[i];                    
    RIniFile.ReadSectionValues(Section, IdentValues); 
    for j := 0 to IdentValues.Count - 1 do begin
      Ident := IdentValues.Names[i];               
      Value := IdentValues.Values[Ident];          
      IniFile.WriteString(Section, Ident, Value);  
    end;
  end;
  Sections.Free; 
  IdentValues.Free;
  IniFile.Free;
  RIniFile.Free;
end;

Best regards,
John, DelphiLand Team

Follow Ups & Related articles