How to display registry in TreeView in Delphi 7

I want to display a tree structure with all the registry data in it (i.e. all subkeys). I did the following Fn to do the same. But I receive information of only one Key, not all. What is missing in my code?

function TForm1.DisplayKeys(TreeNode : TTreeNode;KeyToSearch:String):String; var i: Integer; RootKey : Integer; NewTreeNode : TTreeNode; str : TStringList; // str2: TStringList; begin i:=0; if reg.OpenKey(KeyToSearch,False) then begin str:=nil; str:=TStringList.create; reg.GetKeyNames(str); //For all SubKeys for i:=0 to str.Count-1 do begin NewTreeNode:=TreeView1.Items.AddChild(TreeNode, Str.Strings[i]); if reg.HasSubKeys then begin DisplayKeys(NewTreeNode,Str.Strings[i]); end; end; end; 

function call

  procedure TForm1.FormCreate(Sender: TObject); begin reg:=nil; reg:=TRegistry.create; str2:=nil; str2:=TStringList.create; reg.RootKey:=HKEY_CURRENT_CONFIG; TreeView1.Items.BeginUpdate; //prevents screen repaint every time node is added DisplayKeys(nil,''); // call to fn here TreeView1.Items.EndUpdate; // Nodes now have valid indexes end; 

Please note that I am not getting any error, just this information is incomplete

+6
source share
2 answers

Some problems:

  • You are using OpenKey , which is trying to open a key with write access. Instead, you should use OpenKeyReadOnly . If you really want to write these keys, you will have to work with administrator rights.
  • You cannot close the keys as soon as you are done with them.

More seriously, using relative registry keys is not enough. I believe that you will need to go all the way to the key. I wrote a small demo console application to show what I mean:

 program RegistryEnumerator; {$APPTYPE CONSOLE} uses Classes, Windows, Registry; var Registry: TRegistry; procedure DisplayKeys(const Key: string; const Depth: Integer); var i: Integer; SubKeys: TStringList; begin if Registry.OpenKeyReadOnly(Key) then begin Try SubKeys := TStringList.Create; Try Registry.GetKeyNames(SubKeys); for i := 0 to SubKeys.Count-1 do begin Writeln(StringOfChar(' ', Depth*2) + SubKeys[i]); DisplayKeys(Key + '\' + SubKeys[i], Depth+1); end; Finally SubKeys.Free; End; Finally Registry.CloseKey; End; end; end; begin Registry := TRegistry.Create; Try Registry.RootKey := HKEY_CURRENT_CONFIG; DisplayKeys('', 0); Readln; Finally Registry.Free; End; end. 
+4
source

try the following: -

 procedure TForm1.Button1Click(Sender: TObject); begin TreeView1.Items.Clear; path := Edit1.Text; // reg.RootKey := HKEY_LOCAL_MACHINE ; TreeView1.Items.BeginUpdate; drawtreeview(nil, path); TreeView1.Items.EndUpdate; end; procedure TForm1.drawtreeview( node: TTreeNode; name: string); var i: Integer; NewTreeNode: TTreeNode; str, str2 : TStringList; reg : TRegistry; begin reg := TRegistry.Create; reg.RootKey := HKEY_LOCAL_MACHINE; i := 0; if reg.OpenKeyReadOnly(name) then begin str := TStringList.create; reg.GetKeyNames(str); for i := 0 to str.Count - 1 do begin NewTreeNode := TreeView1.Items.AddChild(node, str.Strings[i]); if reg.HasSubKeys then begin drawtreeview(NewTreeNode, name + '\' + str.Strings[i]); end else ShowMessage('no sub keys'); end; end; reg.CloseKey; reg.Free; end; 
0
source

Source: https://habr.com/ru/post/901276/


All Articles