Delphi 实现程序开机自动启用

RunOnStartup(self.SoeAppInfo1.AppName, ParamStr(0), chkAutoStart.Checked);

procedure TServerForm.RunOnStartup(sProgTitle, sCmdLine: string;
  bStartup: boolean);
var
  sKey: string;
  reg : TRegIniFile;
begin
  sKey := '';  //sKey := 'Once' if you wish it to only run on the next time you startup.
  if bStartup = false then//If value passed is false, then value deleted from Registry.
  begin
    try
      reg := TRegIniFile.Create('');
      reg.RootKey := HKEY_LOCAL_MACHINE;
      reg.DeleteKey(
        'Software\Microsoft'
        + '\Windows\CurrentVersion\Run'
        + sKey + #0,
        sProgTitle);
      reg.Free;
      exit;
    except//Using Try Except so that if value can not be placed in registry, it
      //will not give and error.
    end;
  end;

  try
    reg := TRegIniFile.Create('');
    reg.RootKey := HKEY_LOCAL_MACHINE;
    reg.WriteString(
      'Software\Microsoft'
      + '\Windows\CurrentVersion\Run'
      + sKey + #0,
      sProgTitle,
      sCmdLine);
    reg.Free;
  except

  end;

end;

 

posted @ 2013-02-24 15:54  神码都在云端  阅读(382)  评论(0编辑  收藏  举报