How to check if a registry key exists

I do not know my mistake. It always goes to the else branch, but the key exists, 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; 
+6
source share
1 answer

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

Other comments:

  • Since you are only reading from the registry, use OpenKeyReadOnly , not OpenKey .
  • Check the return value of OpenKeyReadOnly in case the key does not open.
  • If you really need to do this using HKLM (as you state in the comment), watch out for the confusion of redirecting the registry when starting a 32-bit process on a 64-bit system.
+11
source

Source: https://habr.com/ru/post/946585/


All Articles