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:
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 :)
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