To easiest way to make your programs run on Windows startup is to make an entry in the system registry.
To make you program run on all user profiles you have to change the RootKey variable to HKEY_LOCAL_MACHINE else don’t change it, HKEY_CURRENT_USER is set by default.
If you want your program to run only once at Windows startup then choose the key with the RunOnce subkey else choose the Run subkey.
The valid keys to make the entry in are:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
Here’s the code:
uses
Registry;
with TRegistry.Create do
try
{ If you want your programs to run on all user profiles include this }
RootKey := HKEY_LOCAL_MACHINE;
{ If you want you program to run only once include this }
OpenKey('SoftwareMicrosoftWindowsCurrentVersionRunOnce', True);
{ Else include this }
OpenKey('SoftwareMicrosoftWindowsCurrentVersionRun', True);
(* sProgramName - (string) The name of your program. Could be anything you want.
* sCommandLine - (string) The full path to your program executable with
* commandline options (if any).
*)
WriteString(sProgramName, sCommandLine);
CloseKey;
finally
Free;
end;
Tags: commandline options, executable, hkey current user, hkey local machine, programmatically, runonce, system registry, user profiles, valid keys, windows startup
Merlin’s Delphi Forge
Leave a comment