| Код | uses ShlObj, ActiveX, ComObj
function GetSpecialPath(CSIDL: word): string; var s: string; begin SetLength(s, MAX_PATH); if not SHGetSpecialFolderPath(0, PChar(s), CSIDL, true) then s := ''; result := PChar(s); end;
procedure TForm1.Button1Click(Sender: TObject); begin showmessage(GetSpecialPath($0000)); end;
procedure CreateShotCut(SourceFile, ShortCutName, SourceParams: String); var IUnk: IUnknown; ShellLink: IShellLink; ShellFile: IPersistFile; tmpShortCutName: string; WideStr: WideString; i: Integer; begin IUnk := CreateComObject(CLSID_ShellLink); ShellLink := IUnk as IShellLink; ShellFile := IUnk as IPersistFile;
ShellLink.SetPath(PChar(SourceFile)); ShellLink.SetArguments(PChar(SourceParams)); ShellLink.SetWorkingDirectory(PChar(ExtractFilePath(SourceFile)));
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; ShellFile.Save(PWChar(WideStr),False); end;
procedure TForm1.Button2Click(Sender: TObject); var WorkTable:String; begin CreateShotCut(Application.ExeName, GetSpecialPath($0000)+'\'+ExtractFileName(Application.ExeName), ''); end;
procedure CreateShotCut2(ShortCutName : string); var SL: IShellLink; PF: IPersistFile; LnkName: WideString; begin OleCheck(CoCreateInstance(CLSID_ShellLink, nil, CLSCTX_INPROC_SERVER, IShellLink, SL)); { IShellLink implementers are required to implement IPersistFile } PF := SL as IPersistFile; OleCheck(SL.SetPath(PChar(ShortCutName))); // set link path to proper file { create a path location and filename for link file } LnkName := GetSpecialPath($0000) + '\' + ChangeFileExt(ExtractFileName(ShortCutName), '.lnk'); PF.Save(PWideChar(LnkName), True); // save link file end;
procedure TForm1.Button3Click(Sender: TObject); begin CreateShotCut2(Application.ExeName); end;
|
CreateShotCut - будет пытаться создать ярлык.. пока не создаст CreateShotCut2 - создает 1 ярлык
з.ы. путь к раб столу можно узнать еще и так
| Код | const { Registry key where Folder information is kept } SFolderKey = '\Software\Microsoft\Windows\CurrentVersion\' + 'Explorer\Shell Folders';
function GetFolderLocation(const FolderType: string): string; { Retrieves from registry path to folder indicated in FolderType } begin with TRegistry.Create do try RootKey := HKEY_CURRENT_USER; if not OpenKey(SFolderKey, False) then { open key where shell folder information is kept. } raise ERegistryException.CreateFmt('Folder key "%s" not found', [SFolderKey]); { Get path for specified folder } Result := ReadString(FolderType); if Result = '' then raise ERegistryException.CreateFmt('"%s" item not found in registry', [FolderType]); CloseKey; finally Free; end; end;
// вызов GetFolderLocation('Desktop');
|
|