DelphiFAQ Home Search:
General :: Windows :: Programming :: Windows with Delphi :: Windows Filesystem
File related questions and answers. File operations, attributes, system dialogs, hard disk handling.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

Check whether a user has a shortcut installed

The following routine checks whether a shortcut or a file with a given
name is either on the desktop, in the start menu or in its programs submenu.
It will both check in the user's private desktop/ start menu.. as in
the all-users settings.

The return value shows where the first installation was found,
it may be used as in .FormCreate() at the bottom of the example.

Because shortcuts are just files, it is not case-sensitive.
LinkExists ('SourceCoder') = LinkExists ('sourcecoder')

uses
   Registry;
 
 type
   TInstallationPlace = (le_None, le_CommonDesktop, le_CommonProgs, le_CommonStart,
                                  le_UserDesktop,   le_UserProgs,   le_UserStart);
 
 // check whether a shortcut or a file with name s is either on
// the desktop, in the start menu or in its programs submenu.                                  
function LinkExists (const s : String) : TInstallationPlace;
 var
   cDesktop,
   cProgs,
   cStart,
   uDesktop,
   uProgs,
   uStart : String;
 
   function myExists(const s : String): boolean;
   begin
     // s can be directory or a file, so FileExists() won't do it..
    myExists := FileGetAttr(s) >= 0;
   end;
 
 begin
   // check whether we have the link in All_User's Desktop!
  cDesktop := '';
   cProgs := '';
   cStart := '';
   uDesktop := '';
   uProgs := '';
   uStart := '';
   with TRegistry.Create do
   begin
     RootKey:=HKEY_LOCAL_MACHINE;
     if OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', false) then
     begin
       cDesktop := ReadString('Common Desktop');
       cProgs   := ReadString('Common Programs');
       cStart   := ReadString('Common Start Menu');
     end;
     CloseKey;
     RootKey:=HKEY_CURRENT_USER;
     if OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', false) then
     begin
       uDesktop := ReadString('Desktop');
       uProgs   := ReadString('Programs');
       uStart   := ReadString('Start Menu');
     end;
     CloseKey;
     Free;
   end;
   // check in all 3 places for our link
  Result := le_None;
   s := '\' + s;
   if myExists(cDesktop + s) then Result := le_CommonDesktop
   else
   if myExists(cProgs + s) then Result := le_CommonProgs
   else
   if myExists(cStart + s) then Result := le_CommonStart
   else
   if myExists(cDesktop + ChangeFileExt(s, '.lnk')) then Result := le_CommonDesktop
   else
   if myExists(cProgs + ChangeFileExt(s, '.lnk')) then Result := le_CommonProgs
   else
   if myExists(cStart + ChangeFileExt(s, '.lnk')) then Result := le_CommonStart
   else
   if myExists(uDesktop + s) then Result := le_UserDesktop
   else
   if myExists(uProgs + s) then Result := le_UserProgs
   else
   if myExists(uStart + s) then Result := le_UserStart
   else
   if myExists(uDesktop + ChangeFileExt(s, '.lnk')) then Result := le_UserDesktop
   else
   if myExists(uProgs + ChangeFileExt(s, '.lnk')) then Result := le_UserProgs
   else
   if myExists(uStart + ChangeFileExt(s, '.lnk')) then Result := le_UserStart
 end;
 
 procedure TForm1.FormCreate(Sender: TObject);
 begin
   if LinkExists ('SourceCoder') <> le_None then
     ShowMessage('yes')
   else
     ShowMessage('no');
 end;
 
 

Generated 0:02:12 on Sep 25, 2018