2008. június 30., hétfő

How to loop through the keys in the registry


Problem/Question/Abstract:

I am making a program for editing the registry. It will basically be the same as regedit, but with some added features. What I want to know is how should I go about reading the registry into my TreeView and ListView components and how do I specify which icons to use in the tree view? I know you can load files into a list view using the FindFirst, FindNext and FindClose, but how do you loop through the keys in the registry?

Answer:

Enumerating registry keys:


procedure TForm1.Button1Click(Sender: TObject);
var
  indent: Integer;

  procedure EnumAllKeys(hkey: THandle);
  var
    l: TStringList;
    n: Integer;
  begin
    Inc(indent, 2);
    with TRegistry.Create do
    try
      RootKey := hkey;
      OpenKey(EmptyStr, false);
      l := TStringLIst.Create;
      try
        GetKeynames(l);
        CloseKey;
        for n := 0 to l.Count - 1 do
        begin
          memo1.lines.add(StringOfChar(' ', indent) + l[n]);
          if OpenKey(l[n], false) then
          begin
            EnumAllKeys(CurrentKey);
            CloseKey;
          end;
        end;
      finally
        l.Free
      end;
    finally
      Free;
    end;
    Dec(indent, 2);
  end;
begin
  memo1.Clear;
  memo1.lines.add('Keys under HKEY_CURRENT_USER');
  indent := 0;
  EnumAllKEys(HKEY_CURRENT_USER);
end;

Nincsenek megjegyzések:

Megjegyzés küldése