moalex
7/15/2017 - 2:45 AM

windows-registry-autorun.md

Create autorun registry key

run application with Windows start

To run your application automatically after Windows starts, you just need to create an entry in autorun registry key. Here is how…

First of all, you must decide, if you want to run application for current user only or for all users (user accounts). In first case, use this registry key: [HKCU\Software\Microsoft\Windows\Current\Version\Run]

and for all users, use this key: [HKLM\Software\Microsoft\Windows\Current\Version\Run] This code will make autorun entry for all users:

uses Registry;
...

procedure AddEntryToRegistry;
var key: string;
     Reg: TRegIniFile;
begin
  key := 'SoftwareMicrosoftWindowsCurrentVersionRun';
  Reg := TRegIniFile.Create;
  try
    Reg.RootKey:=HKEY_LOCAL_MACHINE;
    Reg.CreateKey(Key);
    if Reg.OpenKey(Key,False) then Reg.WriteString(key, 'MyApp', 'c:MyApp.exe');
  finally
    Reg.Free;
  end;
end;

To remove this entry, just call this procedure:

uses Registry;
...

procedure RemoveEntryFromRegistry;
var key: string;
     Reg: TRegIniFile;
begin
  key := 'SoftwareMicrosoftWindowsCurrentVersionRun';
  Reg:=TRegIniFile.Create;
try
  Reg.RootKey:=HKey_Local_Machine;
  if Reg.OpenKey(Key,False) then Reg.DeleteValue('MyApp');
  finally
  Reg.Free;
  end;
end;

Sometimes, you just need to autorun your application just once (after first reboot of system). You can use these registry keys to achieve this:

[HKCU\SOFTWARE\Microsoft\Windows\Current\Version\Run\Once]

[HKLM\Software\Microsoft\Windows\Current\Version\Run\Once]

HKLM - HKEY_LOCAL_MACHINE

HKCU - HKEY_CURRENT_USER