How to iterate and get all the keys and subkeys in a registry in Delphi 6?

How to iterate and get all the keys and subkeys in a registry in Delphi 6?
sepknowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dagan HooverDeveloperCommented:
The below code should work fine. But loading all the registry keys at one time will be processor intensive and take a long time, even with threading(although this would help). Your best solution would be to load the first sub level of keys and if a user clicks on one to go down you would then get those new keys.
procedure GetRegistryKeys(const ROOTKEY: HKEY; const InList: TStrings; const StartKey: String = '');
var
  Reg : TRegistry;
  I : Integer;
  NameList : TStringList;
begin
  Reg := TRegistry.Create;
  Try
    Reg.RootKey := ROOTKEY;
    If Reg.OpenKeyReadOnly(StartKey) Then
      begin
      NameList := TStringList.Create;
      Try
        Reg.GetKeyNames(NameList);
        For I := 0 To NameList.Count - 1 Do
          begin
          InList.Add(StartKey + '\' + NameList.Strings[I]);
          GetRegistryKeys(ROOTKEY,InList,StartKey + '\' + NameList.Strings[I]);
        end;
      Finally
        NameList.Free;
      end;
    end;
  Finally
    Reg.Free;
  end;
end;
 
procedure TForm12.Button1Click(Sender: TObject);
var
  RegistryKeys : TStringList;
begin
  RegistryKeys := TStringList.Create;
  RegistryKeys.BeginUpdate;
  GetRegistryKeys(HKEY_USERS,RegistryKeys);
  RegistryKeys.EndUpdate;
  ListBox1.Items.BeginUpdate;
  ListBox1.Items.Assign(RegistryKeys);
  ListBox1.Items.EndUpdate;
  RegistryKeys.Free;
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
use the registry unit


uses Registry;
 
procedure TForm1.ListKeys(BaseKey: string; List: TStrings);
var Reg: TRegistry;
  procedure AddSubKeys(KeyName: string);
  var KeyNames: TStrings;
    I: Integer;
  begin
    List.Add(KeyName);
    KeyNames := TStringList.Create;
    try
      if Reg.OpenKey(KeyName, False) then
      try
        Reg.GetKeyNames(KeyNames);
      finally
        Reg.CloseKey;
      end;
      for I := 0 to KeyNames.Count - 1 do
        AddSubKeys(KeyName + '\' + KeyNames[I]);
    finally
      FreeAndNil(KeyNames);
    end;
  end;
begin
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_CURRENT_USER;
    AddSubKeys(BaseKey);
  finally
    FreeAndNil(Reg);
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  ListKeys(Edit1.Text, Memo1.Lines);
end;

Open in new window

0
Geert GOracle dbaCommented:
ThievingSix,
oops forgot to refresh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.