2007. január 12., péntek

Create a Treeview with Keys from the Registry


Problem/Question/Abstract:

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:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls, Registry;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private

  public

    procedure FillRegBranch(rootkey: hkey; parentkey: string; ParentNode: TTreeNode);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  Node: TTreeNode;
begin
  TreeView1.Items.Clear;
  TreeView1.Items.BeginUpdate;
  Node := TreeView1.Items.AddChild(nil, 'Borland');
  FillRegBranch(HKEY_Local_Machine, 'Software\Borland', Node);
  TreeView1.Items.EndUpdate;
end;

procedure tForm1.FillRegBranch(rootkey: hkey; parentkey: string; ParentNode:
  TTreeNode);
var
  Cnt: Integer;
  StList: TStrings;
  Node: tTreeNode;
  Registry: TRegistry;
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := rootkey;
    if Registry.OpenKey(parentkey, false) then
    begin
      StList := tStringlist.Create;
      try
        Registry.GetKeyNames(StList);
        for Cnt := 0 to StList.count - 1 do
        begin
          Node := TreeView1.Items.addChild(ParentNode, StList.Strings[cnt]);
          if Registry.HasSubKeys then
            FillRegBranch(rootkey, parentkey + '\' + StList.Strings[cnt], node);
        end;
      finally
        StList.Free;
      end;
    end;
  finally
    Registry.Free;
  end;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése