Sunday, April 1, 2012

Put Application on Windows Startup

{You can Drag and Drop your Application to Windows Startup Group to make it start automatically whenever Windows Startup. Or you can programmatically put it in code using Delphi.}

// *********************************************** //
procedure RunOnStartup(sProgTitle : string;
    sCmdLine: string;
    bRunOnce: boolean;
    bRemove: boolean) ;
// *********************************************** //    
 var
   sKey: string;
   Section: string;

 begin
   if (bRunOnce) then
     sKey := 'Once'
   else
     sKey := '';

   Section := 'Software\Microsoft\Windows\CurrentVersion\Run' + sKey + #0;

   with TRegIniFile.Create('') do
     try
       RootKey := HKEY_LOCAL_MACHINE;
       if bRemove then
         DeleteKey(Section, sProgTitle)
       else
         WriteString(Section, sProgTitle, sCmdLine) ;
     finally
       Free;
     end;
 end;

 {
Parameter Description:
    sProgTitle:
        Name of your program.
    sCmdLine:
        This is the full path name to your program.
    bRunOnce:
        True to run the application once, such as intalling first time application.
        False to run the application everytime windows startup.
    bRemove:
        True to remove startup key.

How to use:
RunOnStartup(
    'MyCalculator',
    'My Own Calculator',
    'MyCalc.exe',
    False );
}