如何检查注册表项是否存在 [英] How to check if a registry key exists

查看:306
本文介绍了如何检查注册表项是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的错误在哪里。它总是跳到 else 分支,但是密钥存在,我检查了几次。

I don't know wheres my mistake. It always jumps to the else branch, but the key exist, I checked it several times.

var
  reg : TRegistry;
begin
    with TRegistry.Create do try
      RootKey:=HKEY_CURRENT_USER;
      OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', False);
    if KeyExists('nginx.exe') then begin
      ShowMessage('Ja geht ist da');
      Result := True;
      btnAutostart.ImageIndex := 5
    end
    else begin
      Result := False;
      btnAutostart.ImageIndex := 0;
    end;
    finally
      Free;
    end;
end;


推荐答案

您需要调用 ValueExists 而不是 KeyExists 。密钥是Regedit中显示为文件夹的密钥,但是您正在密钥 HKCU\Software\中寻找名为 nginx.exe 的值。 ..\运行

You need to call ValueExists rather than KeyExists. A key is what appears as a folder in Regedit but you are looking for a value named nginx.exe in the key HKCU\Software\...\Run.

其他一些评论:


  1. 由于您仅从注册表中读取内容,因此请使用 OpenKeyReadOnly 而不是 OpenKey

  2. 检查 OpenKeyReadOnly 的返回值,以防无法打开密钥。

  3. 如果您确实需要这样做与HKLM一起使用(如您在评论中所述),请注意在64位系统上运行32位进程时注册表重定向的混乱。

  1. Since you are only reading from the registry, use OpenKeyReadOnly rather than OpenKey.
  2. Check the return value of OpenKeyReadOnly in case the key cannot be opened.
  3. If you actually need to do this with HKLM (as you state in a comment), watch out for registry redirection confusion when running 32 bit process on 64 bit system.

这篇关于如何检查注册表项是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆