Delphi Tutorials »

Remove an entry from a registry key

 
When Windows starts up, it launches all the applications listed in the RunOnce key and all those in the Run key. Afterwards, the entries of the RunOnce key are deleted automatically by Windows, but these in the Run key are left untouched. But what if you want to remove an entry from the Run key, safely and under program control?


Here's the Delphi source code for deleting an entry from the Software\Microsoft\Windows\CurrentVersion\Run key, located in the HKEY_LOCAL_MACHINE root key:

procedure RemoveFromRunKey(ApTitle: string);
var
  Reg: TRegistry;
  TheKey: string;
  ListOfEntries: TStringList;
  i: integer;
begin
  Reg := TRegistry.Create;
  Reg.RootKey := HKEY_LOCAL_MACHINE;
  TheKey := 'Software\Microsoft\Windows\CurrentVersion\Run';
  // Check if key exist...
  // ...if yes, try to delete the entry for ApTitle
  if not Reg.OpenKey(TheKey, False) then
    ShowMessage('Key not found')
  else begin
    if Reg.DeleteValue(ApTitle) then
      ShowMessage('Removed: ' + ApTitle)
    else
      ShowMessage('Not found: ' + ApTitle);
  end;
  Reg.CloseKey;
  Reg.Free;
end;
The parameter ApTitle is the title of the application, the "name" of the registry entry. This parameter must be the same as what you've set using our tutorial Launching an application at Windows Startup.

Example:

  RemoveFromRunKey('Calculator');

 

TOP :: Source Code and Tutorials :: Crash Course Delphi :: Forum DelphiLand Club
DC Library :: Tips :: Downloads :: Links

© Copyright 1999-2016 
Studiebureau Festraets