Thursday, September 10, 2009

Register/Unregister startup application

Here's a nice function which register or unregister an application from startup registry key(add "Registry" to uses clause)
procedure WindowsAutoStartApp(AppPath, AppTitle: string; bRegister: Boolean);
const
  RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
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, AppPath);
    end;
  finally
    Registry.Free;
  end;
end;

Usage
 Register
  WindowsAutoStartApp('C:\Windows\Notepad', 'Notepad', True);
 Unregister
  WindowsAutoStartApp('C:\Windows\Notepad', 'Notepad', False);

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)