procedure MakeLink(proName,lnkName,Description,position : String);
var
WorkDir, DestName: String;
aReg : TRegistry;
aObj : IUnknown;
MyLink : IShellLink;
MyPFile: IPersistFile;
WFileName: WideString;
begin
//操作注册表:
//键'Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders'
//下的Programs串值指定了Windows启动目录的位置.
aReg := TRegistry.create;
aReg.RootKey:=HKey_Current_User;
aReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);
DestName := aReg.ReadString(position) + '/'+lnkName;
aReg.Closekey;
aObj := CreateComObject(CLSID_ShellLink);
MyLink := aObj as IShellLink;
MyPFile := aObj as IPersistFile;
WorkDir:= ExtractFilePath(proName);
with MyLink do //操作IShellLink类
begin
//设置显示窗口的类型
SetShowCmd(SW_NORMAL);
{ 对DOS程序,一般建议使用SetShowCmd(SW_SHOWMAXIMIZED); }
// 设置命令行参数
SetArguments('');
// 设置描述
SetDescription(pChar(Description));
// 设置程序名称[全路径,如果在系统Path中,可直接为程文件名]
SetPath(pChar(proName));
// 设置工作目录
SetWorkingDirectory(pChar(WorkDir));
end;
//取.LNK文件的位置
CreateDir(ExtractFilePath(DestName));
//建立快捷方式: .LNK文件
WFileName := DestName; //将一个String赋给WideString,转换过程由Delphi自动完成
MyPFile.Save(PWChar(WFileName), False);
end;
举例:
MakeLink(Application.ExeName,'iCompanion.lnk','iCompanion','DeskTop');
MakeLink(Application.ExeName,'iCompanion/iCompanion V2.1.lnk','iCompanion','Programs');