递归搜索注册表



我可以使用以下代码成功查询已知键的值。如何递归搜索子项(在下面的示例中为卸载文件夹中的所有子项)以查找特定数据的值?我的目的是查看是否安装了某些特定程序,如果没有,请安装它。

function
...(omitted)
var
     Res : String;
     begin
      RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionUninstall{92EA4162-10D1-418A-91E1-5A0453131A38}','DisplayName', Res);
      if Res <> 'A Value' then
        begin
        // Successfully read the value
        MsgBox('Success: ' + Res, mbInformation, MB_OK);
        end
    end;

原理很简单,使用RegGetSubkeyNames,您将获得某个键的子项数组,然后您只需迭代该数组并查询所有子项以获取DisplayName值,并将该值(如果有)与搜索的值进行比较。

以下函数显示了实现。请注意,我已经从路径中删除了Wow6432Node节点,因此,如果您确实需要它,请修改代码中的UnistallKey常量:

[Code]
const
  UnistallKey = 'SOFTWAREMicrosoftWindowsCurrentVersionUninstall';
function IsAppInstalled(const DisplayName: string): Boolean;
var
  S: string;
  I: Integer;
  SubKeys: TArrayOfString;
begin
  Result := False;
  if RegGetSubkeyNames(HKEY_LOCAL_MACHINE, UnistallKey, SubKeys) then
  begin
    for I := 0 to GetArrayLength(SubKeys) - 1 do
    begin
      if RegQueryStringValue(HKEY_LOCAL_MACHINE, UnistallKey + '' + SubKeys[I],
        'DisplayName', S) and (S = DisplayName) then
      begin
        Result := True;
        Exit;
      end;
    end;
  end
  else
    RaiseException('Opening the uninstall key failed!');
end;

最新更新