我可以使用下面的代码成功查询已知Key的值。如何递归搜索特定数据值的子键(在我下面的示例中,Uninstall文件夹中的所有子键)?我的目标是查看是否安装了某个特定程序,如果没有安装,则安装它。
function
...(omitted)
var
Res : String;
begin
RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{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;
答案 0 :(得分:4)
原理很简单,使用RegGetSubkeyNames
您将获得某个键的子键数组,然后您只需迭代此数组并查询DisplayName
值的所有子键并比较搜索到的值(如果有的话)。
以下函数显示了实现。请注意,我已从路径中删除了Wow6432Node
节点,因此如果您确实需要它,请修改代码中的UnistallKey
常量:
[Code]
const
UnistallKey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall';
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;