Shell Extension with Delphi

Register the association for every user, write your data to
HKEY_LOCAL_MACHINE\Software\Classes

Register the association for the current user only, write your data to
HKEY_CURRENT_USER\Software\Classes

Source Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
with TRegistry.Create do
try
 
  RootKey := HKEY_CURRENT_USER;
  if OpenKey('\Software\Classes\.test', true) then
     WriteString('', 'MyAppDataFile');
  if OpenKey('\Software\Classes\MyAppDataFile', true) then
    WriteString('', 'Mytest for text file');
  if OpenKey('\Software\Classes\MyAppDataFile\DefaultIcon', true) then
    WriteString('', 'C:\WINDOWS\notepad.exe');
  if OpenKey('\Software\Classes\MyAppDataFile\shell\open\command', true) then
    WriteString('', 'C:\WINDOWS\notepad.exe "%1"');
 
finally
  Free;
end;
 
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0);

This will associate .test files, called "MyTest For Text File" so that they will have the icon of notepad.exe and will be opened bynotepad.exe. The last line tells Explorer to 'reload' itself to reflect the changes made to the file associations. For instance, Explorer file list views will update. The WinAPI function SHChangeNotify is declared in ShlObj.pas, so you need to uses ShlObj.
Notice that the %1 in shell\open\command will expand to the current file. For instance, if you double-click on C:\some dir\atxtfile.test, then Explorer will execute the command.
C:\WINDOWS\notepad.exe "C:\some dir\atxtfile.test"

Post a Comment