Wednesday, August 17, 2011

Run On Startup


// uses TRegistry
procedure SetRunOnStartup(sProgTitle, sCmdLine: string;
RunType: TRunOnStartupAction);
var
reg: TRegIniFile;
sStartupRegKey: string;
begin
try
  sStartupRegKey := 'Software\Microsoft\Windows\CurrentVersion\Run';
  reg := TRegIniFile.Create('');
  reg.RootKey := HKEY_CURRENT_USER; //works for win7
  if RunType = rsRunOnce then
    reg.WriteString(sStartupRegKey + 'Once'#0, sProgTitle, sCmdLine)
  else if RunType = rsRunAllways then
    reg.WriteString(sStartupRegKey + #0, sProgTitle, sCmdLine)
  else
    reg.DeleteKey(sStartupRegKey + #0, sProgTitle);
  reg.Free;
except
  ShowMessage('Error!');
end;
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
SetRunOnStartup(Application.Title, Application.ExeName, rsRunAllways);
Showmessage('Program will run on windows startup!');
end;

procedure TForm1.ToolButton2Click(Sender: TObject);
begin
SetRunOnStartup(Application.Title, Application.ExeName, rsRunNever);
Showmessage('Program will NOT run on windows startup!');
end;

No comments: