0

How to make my program edit the registry so it can start on the computer startup? using

TRegistry

2 Answers 2

5

Have a look at the following location in the registry:

HKLM\Software\Microsoft\Windows\CurrentVersion\Run

You can use TRegistry to add entries here.

10
  • Thank you . If I could I would have +1 you :D
    – HTxL
    Feb 7, 2012 at 20:26
  • 1
    To avoid the next post, it is better to use HKCU instead.
    – OnTheFly
    Feb 7, 2012 at 20:30
  • It depends if you want it to run for one (HKCU) or all (HKLM) Feb 7, 2012 at 20:31
  • From what I understand HKLM will make all of the users on this computer to start this program up while HKCU will make only the current active user to start it up
    – HTxL
    Feb 7, 2012 at 20:32
  • 1
    What @user539484 means is that you program will typically not have write access to HKLM. So you will either need to gain write access to HKLM, or write under the HKCU registry key. In that case though, your program will only start upon logon for the user that was logged on when the reg key was written. The bottom line with all of this is that such a change is best done when your install program runs because that's when you can reasonably expect to have write access to HKLM. Feb 7, 2012 at 20:33
2

hey try this code to start your application on the next startup

    procedure RunOnStartupHKCU(const sCmdLine: string;) ;
       var
       sKey              : string;
       Section           : string;
       ApplicationTitle  : string;
       begin
        ApplicationTitle:='myApplication';
        sKey := 'Once'
        Section := 'Software\Microsoft\Windows\CurrentVersion\Run' + sKey + #0;

       with TRegIniFile.Create('') do
         try
           RootKey := HKEY_CURRENT_USER;
           WriteString(Section, ApplicationTitle, sCmdLine) ;
         finally
         Free;
        end;
       end;

To call the it use this

     sAppNam:=Application.ExeName;
     RunOnStartupHKCU(sAppNam) ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.