2010. március 11., csütörtök

Check whether a user has a shortcut installed


Problem/Question/Abstract:

Check whether a user has a shortcut installed

Answer:

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;

Nincsenek megjegyzések:

Megjegyzés küldése