16,748
社区成员
发帖
与我相关
我的任务
分享
procedure TForm1.CreateLinkFile(SourceFile, ShortCutName, Description: string);
var
IUnk: IUnknown;
ShellLink: IShellLink;
IPFile: IPersistFile;
tmpShortCutName: string;
WideStr: WideString;
i: integer;
begin
if SUCCEEDED(CoInitialize(nil)) then
begin
IUnk := CreateComObject(CLSID_ShellLink);
ShellLink := IUnk as IShellLink;
IPFile := IUnk as IPersistFile;
with ShellLink do
begin
SetPath(PChar(SourceFile)); //执行程序的文件名
SetDescription(PChar(Description)); //提示说明文本
SetWorkingDirectory(PChar(ExtractFilePath(SourceFile))); //启动目录
end;
ShortCutName := ChangeFileExt(ShortCutName, '.lnk');
if FileExists(ShortCutName) then //如果文件名存在,就以数据序号来重新命名一个新的文件名
begin
ShortCutName := Copy(ShortCutName, 1, Length(ShortCutName) - 4);
i := 1;
repeat
tmpShortCutName := ShortCutName + '(' + IntToStr(i) + ').lnk';
Inc(i);
until not FileExists(tmpShortCutName);
WideStr := tmpShortCutName;
end
else WideStr := ShortCutName;
IPFile.Save(PWChar(WideStr), False);
CoUninitialize;
end;
end;