Author: Lou Adler
Anyone have any sample code on how to load a TreeView with registry keys, i want
to load the
KEY_CURRENT_USER\\Software key, and i want all the subkeys to load in the treeview
too.
Answer:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls, ComCtrls, Registry;
8
9 type
10 TForm1 = class(TForm)
11 TreeView1: TTreeView;
12 Button1: TButton;
13 procedure Button1Click(Sender: TObject);
14 private
15
16 public
17
18 procedure FillRegBranch(rootkey: hkey; parentkey: string; ParentNode:
19 TTreeNode);
20 end;
21
22 var
23 Form1: TForm1;
24
25 implementation
26
27 {$R *.DFM}
28
29 procedure TForm1.Button1Click(Sender: TObject);
30 var
31 Node: TTreeNode;
32 begin
33 TreeView1.Items.Clear;
34 TreeView1.Items.BeginUpdate;
35 Node := TreeView1.Items.AddChild(nil, 'Borland');
36 FillRegBranch(HKEY_Local_Machine, 'Software\Borland', Node);
37 TreeView1.Items.EndUpdate;
38 end;
39
40 procedure tForm1.FillRegBranch(rootkey: hkey; parentkey: string; ParentNode:
41 TTreeNode);
42 var
43 Cnt: Integer;
44 StList: TStrings;
45 Node: tTreeNode;
46 Registry: TRegistry;
47 begin
48 Registry := TRegistry.Create;
49 try
50 Registry.RootKey := rootkey;
51 if Registry.OpenKey(parentkey, false) then
52 begin
53 StList := tStringlist.Create;
54 try
55 Registry.GetKeyNames(StList);
56 for Cnt := 0 to StList.count - 1 do
57 begin
58 Node := TreeView1.Items.addChild(ParentNode, StList.Strings[cnt]);
59 if Registry.HasSubKeys then
60 FillRegBranch(rootkey, parentkey + '\' + StList.Strings[cnt], node);
61 end;
62 finally
63 StList.Free;
64 end;
65 end;
66 finally
67 Registry.Free;
68 end;
69 end;
70
71 end.
|