- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

Running apps on Windows startup

Question:

How can I get my application to run every time Windows starts?

Answer:

The following demonstrates how to add your application to the

Windows boot sequence. Code is demonstrated for both the Win32 and

Win16 platforms.



uses

  Registry, {For Win32}

  IniFiles; {For Win16}



{$IFNDEF WIN32}

  const MAX_PATH = 144;

{$ENDIF}



{For Win32}

procedure TForm1.Button1Click(Sender: TObject);

var

  reg: TRegistry;

begin

  reg := TRegistry.Create;

  reg.RootKey := HKEY_LOCAL_MACHINE;

  reg.LazyWrite := false;

  reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',

              false);

  reg.WriteString('My App', Application.ExeName);

  reg.CloseKey;

  reg.free;

end;





{For Win16}

procedure TForm1.Button2Click(Sender: TObject);

var

  WinIni : TIniFile;

  WinIniFileName : array[0..MAX_PATH] of char;

  s : string;

begin

  GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName));

  StrCat(WinIniFileName, '\win.ini');

  WinIni := TIniFile.Create(WinIniFileName);

  s := WinIni.ReadString('windows',

                         'run',

                         '');

  if s = '' then

    s := Application.ExeName else

    s := s + ';' + Application.ExeName;

  WinIni.WriteString('windows',

                     'run',

                     s);

  WinIni.Free;

end;




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile