...run a Program automatically at Windows start-up?

Author: Thomas Stutz

Category: Files

uses
  
Registry;

procedure SetAutoStart(AppName, AppTitle: string; bRegister: Boolean);
const
  
RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
  // or: RegKey = '\Software\Microsoft\Windows\CurrentVersion\RunOnce';
var
  
Registry: TRegistry;
begin
  
Registry := TRegistry.Create;
  try
    
Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKey(RegKey, False) then
    begin
      if 
bRegister = False then
        
Registry.DeleteValue(AppTitle)
      else
        
Registry.WriteString(AppTitle, AppName);
    end;
  finally
    
Registry.Free;
  end;
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  
// 1.Parameter: Path to your Exe-File
  // 2. Parameter: the Title of your Application
  // 3. Set (true) or Unset (false) Autorun
  
SetAutoStart(ParamStr(0), 'Title of your Application', True);
end;

 

printed from
www.swissdelphicenter.ch
developers knowledge base