How to create a file shortcut (*.lnk file) on desktop in Windows?

前端 未结 3 2239
清歌不尽
清歌不尽 2021-02-08 03:39
function GetDesktopFolder: string;
var
  buf: array[0..MAX_PATH] of Char;
  pidList: PItemIDList;
begin
  Result := StrNoDesktopFolderFo;
  SHGetSpecialFolderLocation(Ap         


        
3条回答
  •  囚心锁ツ
    2021-02-08 04:01

    uses ShlObj, ActiveX, ComObj;
    ...
    procedure TForm1.Button1Click(Sender: TObject);
    var
    IObject : IUnknown;
    ISLink : IShellLink;
    IPFile : IPersistFile;`enter code here`
    PIDL : PItemIDList;
    InFolder : array[0..MAX_PATH] of Char;
    TargetName : String;
    LinkName : WideString;
    begin
    TargetName := 'C:\Windows\System32\calc.exe';
    
    {Use TargetName:=ParamStr(0) which
    returns the path and file name of the
    executing program to create a link to your
    Application}
    
    IObject := CreateComObject(CLSID_ShellLink);
    ISLink := IObject as IShellLink;
    IPFile := IObject as IPersistFile;
    
    with ISLink do begin
    SetPath(pChar(TargetName));
    SetWorkingDirectory
    (pChar(ExtractFilePath(TargetName)));
    end;
    
    // if we want to place a link on the Desktop
    SHGetSpecialFolderLocation
    (0, CSIDL_DESKTOPDIRECTORY, PIDL);
    SHGetPathFromIDList
    (PIDL, InFolder);
    
    {
    or if we want a link to appear in
    some other, not-so-special, folder:
    InFolder := 'c:\SomeFolder'
    }
    
    LinkName := InFolder + '\Delphi Created Link.lnk';
    IPFile.Save(PWChar(LinkName), false);
    end;
    

    Source :[http://www.delphipages.com/forum/showthread.php?t=46623][1]

提交回复
热议问题