DelphiFAQ Home Search:

Check whether a user has a shortcut installed

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

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;


Comments:

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.