Adding An Item To The Shell's Context Menu

Coordinator
Oct 22, 2008 at 9:43 AM
Edited Oct 22, 2008 at 9:44 AM

I wanted this application to be as easy to use as possible. I decided it would be nice to have two context menu items on the Windows Explorer file context menu, so that when the user right clicks on any file in Windows Explorer, they would be able to select "Encrypt File..." and "Decrypt File...", and it would transform the contents of the file and not create a new one, or change the old one's file name (a sort of pseudo-in-place transform).

There are two options when adding to the shell's context menu:

  • Static - This is the easiest way to add menu items, and requires only a few simple registry entries.
  • Dynamic - This requires a lot more effort. A dynamic context menu item requires you to create an ActiveX control that implements the IShellExtInit and IContextMenu interfaces.

Although dynamic menu items offer much more functionality, I didn't need any of it, and so I opted for the static route.

As an example, the following explains how to set up a static context menu item that will appear whenever a user right clicks any file, that will allow them to open the file in Notepad:

  1. Open regedit (Start -> Run => regedit).
  2. Select HKEY_CLASSES_ROOT\*\shell\ and create a new key (called something like NotepadEdit).
  3. Set the default value of this key to what you want to be shown on the context menu item (e.g. "Open with Notepad").
  4. Create a new key under this one called "Command" (e.g. HKEY_CLASSES_ROOT\*\shell\NotepadEdit\Command\).
  5. Set the default value of this key to "notepad.exe %1".
  6. Now, see if it works, by right clicking a file (not a folder) in Windows Explorer, and clicking "Open with Notepad" (if that's what you called it). The file you chose should open in Notepad.

To add "Encrypt File" and "Decrypt File" to the shell's context menu, I needed to add two sets of registry keys (one for encryption, the other for decryption).

I created these:

  • HKEY_CLASSES_ROOT\*\shell\WinCryptor2008Encrypt\ - for encryption
    • Set default value to "Encrypt File...".
    • Create new key at HKEY_CLASSES_ROOT\*\shell\WinCryptor2008Encrypt\Command.
    • Set the new Command key's default value to <path to wincryptor2008.exe> %1 1.
  • HKEY_CLASSES_ROOT\*\shell\WinCryptor2008Decrypt\ - for decryption
    • Set default value to "Decrypt File".
    • Create new key at HKEY_CLASSES_ROOT\*\shell\WinCryptor2008Decrypt\Command.
    • Set the new Command key's default value to <path to wincryptor2008.exe> %1 0.

These keys are added during installation using the MSI file that you can get on the release page, so that the user doesn't need to.

The 1 and 0 at the end of the Command key's default values indicate encryption and decryption respectively.