In the previous article we began to write a simple
application. Here is the continuation.
Introduction.
In the previous article (see
http://www.suite101.com/article.cfm/delp... we began to
write a program that reads a system registry key and all its
values, stores key and values into a collection, and lists them
to user. The collection can be saved to a file and loaded from
it. It consists of two units ILRegCollection and Unit1.
ILRegCollection unit that implements component (TILRegSaver)
and collection (TILRegEntryList) classes was discussed in the
previous article.
procedure TForm1.ListItems(Item:TILRegEntry);
var
i:integer;
begin
Memo1.Lines.Add('EntryName:'#9+Item.EntryName);
if Item.Value<>NULL then
Memo1.Lines.Add('value:'#9#9+item.value);
for i:=0 to item.SubEntries.Count-1 do
ListItems(item.SubEntries[i]);
end;
procedure TForm1.readFromRegistry(Saver: TILREGSaver);
var
r:TRegistry;
item,item1:TILRegEntry;
s:TStringList;
vInfo:TRegDataInfo;
i:integer;
begin
r:=TRegistry.create;
s:=TStringList.Create;
try
r.RootKey:=HKEY_LOCAL_MACHINE;
if not r.OpenKey(edKeyName.text,false) then
raise Exception.Create('Path not found!');
if not r.GetDataInfo(',vInfo) then
raise Exception.Create(');
item:=TILREGENTRY(Saver.RegEntrys.Add);
item.EntryName:=edKeyName.text;
item.EntryType:=rdUnknown;
item.Value:=NULL;
r.GetValueNames(s);
for i:=0 to s.Count-1 do
begin
item1:=TILREGENTRY(item.SubEntries.add);
try
item1.EntryName:=s[i];
if not r.GetDataInfo(item1.EntryName,vInfo) then
raise Exception.Create(');
item1.EntryType:=vInfo.RegData;
case item1.EntryType of
rdString,
rdExpandString:
item1.Value:=r.ReadString(item1.EntryName);
rdInteger:
item1.Value:=r.ReadInteger(item1.EntryName);
else
item1.Value:=NULL;
end;
except
item1.Free;
end;
end;
finally
r.free;
s.free;
end;
end;
// event handlers
procedure TForm1.FormCreate(Sender: TObject);
begin
FSaver:=TILREGSaver.create(nil);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FSaver.free;
end;
procedure TForm1.btnReadClick(Sender: TObject);
begin
readFromRegistry(FSAVER);
end;
procedure TForm1.btnListClick(Sender: TObject);
var
i:integer;
begin
for i:=0 to FSaver.RegEntrys.Count-1 do
ListItems(FSaver.RegEntrys[i]);
end;
procedure TForm1.btnLoadClick(Sender: TObject);
begin
FSaver.LoadFromFile(ChangeFileExt(Paramstr(0),'.IL'));
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
FSaver.SaveToFile(ChangeFileExt(Paramstr(0),'.IL'));
end;
end.
Code explanation.
The form looks like following:
Important methods:
1. procedure FormCreate(Sender: TObject);
This method is called just after a form is created; it creates an
instance of TILRegSaver component and store pointer into FSaver
variable.
2. procedure FormDestroy(Sender: TObject);
This method is called just before a form is destoyed; it frees an
instance of TILRegSaver component that was created in the
FormCreate Method.
3. procedure btnLoadClick(Sender: TObject);
This method is called when user clicks "Load From File"
button; it calls LoadFromFile method of FSaver in order to load
the collection of registry keys and its values from the file
"project1.il".
4. procedure btnSaveClick(Sender: TObject);
This method is called when user clicks "Save To File"
The copyright of the article More collections. Part 2. in Delphi Programming is owned by . Permission to republish More collections. Part 2. in print or online must be granted by the author in writing.