Author: Tomas Rutkauskas
Imagine there are two win.ini files and I want to combine both together. Some of
the common sections and keys/ values are the same in each but they differ in their
data generally. How could I merge the two together to get an ini file that contains
all the data from both in the right places?
Answer:
Iterate over the sections in the source file. For each section, iterate over the
names. If there is a name in Source that already exists in Dest, Dest's copy will
be overwritten.
1 { ... }2 var3 Source, Dest: TIniFile;
4 SectionNames: TStrings;
5 i: Integer;
6 begin7 SectionNames := TStringList.Create;
8 try9 Source.ReadSections(SectionNames);
10 for i := 0 to SectionNames.Count - 1 do11 begin12 MergeSection(Source, Dest, SectionNames[i]);
13 end;
14 finally15 SectionNames.Free;
16 end;
17 end;
18 19 procedure MergeSection(Source, Dest: TIniFile; const SectionName: string);
20 var21 i: Integer;
22 Section: TStrings;
23 Name, Value: string;
24 begin25 Section := TStringList.Create;
26 try27 Source.ReadSection(SectionName, Section);
28 for i := 0 to Section.Count - 1 do29 begin30 Name := Section.Names[i];
31 Value := Section.Values[Name];
32 Dest.WriteString(SectionName, Name, Value);
33 end;
34 finally35 Section.Free;
36 end;
37 end;