How to add a context menu item to Windows Explorer #15

To add a context menu item to Windows Explorer you have create a few entries in the system registry. First you have to get the file type's name, add the context menu item and add then the command line. Besides files you can add context menus to drives or folders.

Special extensions:

  • drive - drives.
  • folder - folders.
uses
  Registry;

...
var
  sTypeName: string;
begin
  with TRegistry.Create do
    try
      RootKey := HKEY_CLASSES_ROOT;
      { Set the extension you want. Include the '.' character }
      OpenKey('.Extension', True);
      { Get file type name }
      sTypeName := ReadString('');
      CloseKey;
      { Create explorer extension. Instead of 'Open' put whatever you want. }
      OpenKey('' + sTypeName + 'ShellOpen', True);
      { The context menu item }
      WriteString('', 'Open in my application');
      CloseKey;

      OpenKey('' + sTypeName + 'ShellOpenCommand', True);
      { The commandline. Enter all the commandline options you need.
        Remember to use quotes for long file names with spaces. Even if your
        application's filename doesn't contain any spaces the folder that
        it's in may contain them.
        %1 - is the selected file's name. }
      WriteString('', '"' + Application.ExeName + '" "%1"');
      CloseKey;

      { Repeat this for every file type you want to add a context menu to.
        For this operation you may want to use a for loop and a constant
        string array with the file extensions. }
    finally
      Free;
    end;
end;
Author: Unknown
Added: 2007/06/02
Last updated: 2007/06/02