Author: William Gerbert
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')
1 uses
2 Registry;
3
4 type
5 TInstallationPlace = (le_None, le_CommonDesktop, le_CommonProgs, le_CommonStart,
6 le_UserDesktop, le_UserProgs, le_UserStart);
7
8 // check whether a shortcut or a file with name s is either on
9 // the desktop, in the start menu or in its programs submenu.
10
11 function LinkExists(const s: string): TInstallationPlace;
12 var
13 cDesktop,
14 cProgs,
15 cStart,
16 uDesktop,
17 uProgs,
18 uStart: string;
19
20 function myExists(const s: string): boolean;
21 begin
22 // s can be directory or a file, so FileExists() won't do it..
23 myExists := FileGetAttr(s) >= 0;
24 end;
25
26 begin
27 // check whether we have the link in All_User's Desktop!
28 cDesktop := '';
29 cProgs := '';
30 cStart := '';
31 uDesktop := '';
32 uProgs := '';
33 uStart := '';
34 with TRegistry.Create do
35 begin
36 RootKey := HKEY_LOCAL_MACHINE;
37 if OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
38 false) then
39 begin
40 cDesktop := ReadString('Common Desktop');
41 cProgs := ReadString('Common Programs');
42 cStart := ReadString('Common Start Menu');
43 end;
44 CloseKey;
45 RootKey := HKEY_CURRENT_USER;
46 if OpenKey('\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
47 false) then
48 begin
49 uDesktop := ReadString('Desktop');
50 uProgs := ReadString('Programs');
51 uStart := ReadString('Start Menu');
52 end;
53 CloseKey;
54 Free;
55 end;
56 // check in all 3 places for our link
57 Result := le_None;
58 s := '\' + s;
59 if myExists(cDesktop + s) then
60 Result := le_CommonDesktop
61 else if myExists(cProgs + s) then
62 Result := le_CommonProgs
63 else if myExists(cStart + s) then
64 Result := le_CommonStart
65 else if myExists(cDesktop + ChangeFileExt(s, '.lnk')) then
66 Result := le_CommonDesktop
67 else if myExists(cProgs + ChangeFileExt(s, '.lnk')) then
68 Result := le_CommonProgs
69 else if myExists(cStart + ChangeFileExt(s, '.lnk')) then
70 Result := le_CommonStart
71 else if myExists(uDesktop + s) then
72 Result := le_UserDesktop
73 else if myExists(uProgs + s) then
74 Result := le_UserProgs
75 else if myExists(uStart + s) then
76 Result := le_UserStart
77 else if myExists(uDesktop + ChangeFileExt(s, '.lnk')) then
78 Result := le_UserDesktop
79 else if myExists(uProgs + ChangeFileExt(s, '.lnk')) then
80 Result := le_UserProgs
81 else if myExists(uStart + ChangeFileExt(s, '.lnk')) then
82 Result := le_UserStart
83 end;
84
85 procedure TForm1.FormCreate(Sender: TObject);
86 begin
87 if LinkExists('SourceCoder') <> le_None then
88 ShowMessage('yes')
89 else
90 ShowMessage('no');
91 end;
|