Read unknown registry Key

How would I read a registry key in delphi with a wildcard or similar if I don't know the whole key name.

I know the whole path to the key and the first part of the key name but the ending will not allways be the same.
LVL 3
angel_fire2701Asked:
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.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Assuming '*' as wildcard you could do something like this


uses
Registry;

procedure TForm1.Button1Click(Sender: TObject);
function FindRegistryKey(RootKey: HKEY;
                               RootPath: String;
                               KeyWithWildCard: String): String;
var
  Reg: TRegistry;
  Subkeys: TStrings;
  I: Integer;
  PrefixKey: String;
begin
  I := 1;
  while i < length(KeyWithWildCard) do
   begin
    if KeyWithWildCard[i] = '*' then break;
    PrefixKey := PrefixKey+KeyWithWildCard[i];
    inc(i);
   end;
  Result := '';
  Reg := TRegistry.Create;
  Subkeys := TStringList.Create;
  try
    Reg.RootKey := RootKey;
    if (Reg.OpenKey(RootPath, False)) then
      begin
      Reg.GetKeyNames(Subkeys);
      for I := 0 to (Subkeys.Count - 1) do
          begin
          If pos(PrefixKey,Subkeys[I]) > 0 then
          Result := RootPath + '\' + Subkeys[I];
          if (Result <> '') then
            Break;
          end;
      end;
  finally
    Subkeys.Free;
    Reg.Free;
  end;
end;
begin
 Showmessage(FindRegistryKey(HKEY_LOCAL_MACHINE,'SOFTWARE\Microsoft\Windows\CurrentVersion','Windows*'));
end;

Open in new window

0
angel_fire2701Author Commented:
Thanks for the reply.
I created my form, added a button then went into the source and pasted in the code you provided.
As you can see I changed the HKEY to current user and as a test I tried to pull the path key for 7-zip using a wildcard with no success.
I think were on the right track, do you think I'm missing something?

Capture.JPG
0
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
You said that you was looking for a key, not a value...

This code will retrieve the value
procedure TForm1.Button1Click(Sender: TObject);
function FindRegistryValue(RootKey: HKEY;
                               RootPath: String;
                               ValueWithWildCard: String): String;
var
  Reg: TRegistry;
  Subkeys: TStrings;
  I: Integer;
  PrefixKey: String;
begin
  I := 1;
  while i < length(ValueWithWildCard) do
   begin
    if ValueWithWildCard[i] = '*' then break;
    PrefixKey := PrefixKey+ValueWithWildCard[i];
    inc(i);
   end;
  Result := '';
  Reg := TRegistry.Create;
  Subkeys := TStringList.Create;
  try
    Reg.RootKey := RootKey;
    if (Reg.OpenKey(RootPath, False)) then
      begin
      Reg.GetValueNames(Subkeys);
      for I := 0 to (Subkeys.Count - 1) do
          begin
          If pos(PrefixKey,Subkeys[I]) > 0 then
          Result := RootPath + '\' + Subkeys[I];
          if (Result <> '') then
            Break;
          end;
      end;
  finally
    Subkeys.Free;
    Reg.Free;
  end;
end;
begin
 Showmessage(FindRegistryValue(HKEY_CURRENT_USER,'SOFTWARE\7-zip','Pa*'));
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
angel_fire2701Author Commented:
yes sorry if i didn't elaborate, I need the value of an partially known key. I'm off for the weekend and will return to this on Monday, thanks for your help so far.
0
angel_fire2701Author Commented:
Thank You, That works Perfectly.
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.