频道分类

Delphi 设置系统启动文件夹自身开机自动运行

作者:admin 来源: 日期:2019/10/8 12:10:13 人气: 标签:

 

 uses  ShlObj, ShellAPI,System.IniFiles, System.UITypes;


 function GetAutoRunFilePath(): String;
var
  fileName, pathStartUp: String;
  path: array [0 .. 255] of Char;
begin
  fileName := Application.title + '.url';  //url
  if SHGetSpecialFolderPath(0, @path[0], CSIDL_STARTUP, true) then
    pathStartUp := String(path);

  Result := pathStartUp + PathDelim + fileName;
end;

 procedure CreateAutoRun();
const
  FileProtocol = 'file:///';
var
  fileName: String;
  pathExe, pathUrl, pathAutoRun: String;
begin
  fileName := Application.title + '.url'; //url
  pathUrl := application.Exename;
  pathExe := ParamStr(0);

  pathAutoRun := GetAutoRunFilePath();

  with TIniFile.Create(pathAutoRun) do
    try
      WriteString('InternetShortcut', 'URL', FileProtocol + pathExe);
      WriteString('InternetShortcut', 'IconIndex', '0');
      WriteString('InternetShortcut', 'IconFile', pathExe);
    finally
      DisposeOf();
    end;

end;



procedure TForm1.FormCreate(Sender: TObject);
begin
CreateAutoRun();  //调用演示
end;