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: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
and for all users, use this key: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
This code will make autorun entry for all users:
uses Registry; ... procedure AddEntryToRegistry; var key: string; Reg: TRegIniFile; begin key := '\Software\Microsoft\Windows\CurrentVersion\Run'; 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 := '\Software\Microsoft\Windows\CurrentVersion\Run'; 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:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
Entries (RSS)






Posted
on
Saturday, June 16th, 2007 at 5:05 pm under

Thank you for this code. One question: How could I change:
‘c:\MyApp.exe’
to something like this:
‘ExtractFilePath(Application.ExeName)\MyApp.exe’
Thanks
August 13th, 2008 at 5:18 pmto gord: Sorry, but I don’t understand your question. You can do it exactly as you mentioned.:-)
You can use something like this:
if Reg.OpenKey(Key,False) then Reg.WriteString(key, ‘MyApp’, Application.ExeName);
August 13th, 2008 at 7:12 pmHeh. That’s why you’re admin and I just…well, you know
BTW, this is awesome site - I find a lot of tips not available on other Delphi tips sites. Hat down.
gord
August 13th, 2008 at 10:35 pm