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?
 
Ferruccio AccalaiConnect With a Mentor Senior 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
 
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
 
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
All Courses

From novice to tech pro — start learning today.