使用代码创建快捷方式

使用代码创建一个应用程序的快捷方式, 主要是用了IShellLink这个接口, 调用很简单

uses
  ShlObj, ComObj, ActiveX;



{参数说明
AFile: 执行文件(含全路径)
AArguments: 启动参数
ALinkCaption: 快捷方式名称
ADescription: 快捷方式描述
ALinkPath: 快捷方式目录}
procedure CreateLinkFile(AFile, AArguments, ALinkCaption, ADescription: string;
  ALinkPath: String = '');
var
  nIShellLink: IShellLink;
  nIPFile: IPersistFile;
  nLKFile: string;
  i: integer;
begin
  if SUCCEEDED(CoInitialize(nil)) then
  Try
    nIShellLink := CreateComObject(CLSID_ShellLink) as IShellLink;
    nIPFile  := nIShellLink as IPersistFile;

    if ALinkPath = '' then
      ALinkPath := ExtractFilePath(AFile);

    with nIShellLink do
    begin
      SetPath(PChar(AFile)); //执行程序的文件名
      SetDescription(PChar(ADescription)); //提示说明文本
      SetWorkingDirectory(PChar(ExtractFilePath(AFile))); //启动目录
      SetArguments(PChar(AArguments));
    end;

    nLKFile := ALinkPath + ALinkCaption + '.lnk';
    if FileExists(nLKFile) then //如果文件名存在,就以数据序号来重新命名一个新的文件名
    begin
      i := 1;
      repeat
        nLKFile := ALinkPath + ALinkCaption + '(' + IntToStr(i)+ ').lnk';
        Inc(i);
      until not FileExists(nLKFile);
    end;

    nIPFile.Save(PWChar(WideString(nLKFile)), False);
  finally
    CoUninitialize;
  end;
end;
原文地址:https://www.cnblogs.com/lzl_17948876/p/3461731.html