Add menu item to Windows context menu only for specific filetype

Windows XpRegistryContextmenu

Windows Xp Problem Overview


I've developed an application that load an image using the context menu of window (right click on the file) and for the moment is working, but the reg key is on

HKEY_CLASSES_ROOT\*

and it works with all files.

I want that the menu item on the context menu should be displayed only with .jpg files.

How can I do that? Which registry keys should I use?

Windows Xp Solutions


Solution 1 - Windows Xp

  1. Identify the file type (ProgID) for .jpg files

    This can be done by checking the default value of HKEY_CLASSES_ROOT\.jpg. It could be anything based on what you've installed, but for the purposes of this example, we'll call it jpegfile, a common default.

  2. Set the context menu item (verb) properties for that file type

    You can set per-user context menu items in HKEY_CURRENT_USER\Software\Classes\jpegfile\shell. This key has a list of verbs for the file type. There is a similar key in HKEY_LOCAL_MACHINE\Software\Classes\jpegfile\shell, and these are the system defaults for the file type. You can put a verb key there too, but if the same key exists in HKCU, it will be overridden, so be advised.

  3. Set the command value

    The bare minimum key value that needs to be set to get it to work is the default value of the command subkey. You need to set that with the path to your application, like so: HKEY_CURRENT_USER\Software\Classes\jpegfile\shell\open_with_myapp\command would be set to "c:\path\to\myapp.exe" "%1". Now a context menu for .jpg files will have a "open_with_myapp" item which will launch your app when clicked, and pass the file name of the selected file as a parameter. Of course, how your application processes parameters is up to you, so you'd need to set the parameter string to something your app can process.

  4. Set other verb properties

    I'd imagine you're probably going to want the context menu item to read something a little more friendly than the key name. You can have the context menu display whatever label you want for your item by setting the default value of that key (open_with_myapp).

That's your basic overview. Definitely check out my answer to this question about associating a file, which has a similar answer:

Solution 2 - Windows Xp

There's another key on the registry that works independently of user's default programs: HKEY_CLASSES_ROOT\SystemFileAssociations. Since nobody mentioned it on this question... No need to check ProgID before adding the context menu item. Example:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\subtitle]
@="Search subtitles..."

[HKEY_CLASSES_ROOT\SystemFileAssociations\.mkv\shell\subtitle\command]
@="\"D:\\Tools\\subsearch.exe\" \"%1\""

Reference: https://docs.microsoft.com/en-us/windows/desktop/shell/app-registration#registering-verbs-and-other-file-association-information

Additional Considerations:

The HKEY_CLASSES_ROOT subtree can be written to but in general is a view formed by merging

  • HKEY_CURRENT_USER\Software\Classes
    • file type registration visible to the current user only
  • HKEY_LOCAL_MACHINE\Software\Classes
    • globally register a file type on a particular computer

You can register to those classes instead/aswell

The (ProgID) defined verbs have priority over the same ones defined in ...\SystemFileAssociations\ , but are dependent on that particular Application, When that application uninstalls, it would normally delete its registry entry, along with what modifications/additions you may have done under that key. Or if the default (ProgID) is changed, your modifications will no longer be in effect.

The ...\SystemFileAssociations\ registrations are stable even when users change/uninstall the default programs.

Solution 3 - Windows Xp

Will publish my working solution derived from the previous answer (and one of its author's other answer). It also adds an icon. I used it for all file types and didn't have administrator's privileges. The subitem * didn't exist in my registry, I created it myself.

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\*]

[HKEY_CURRENT_USER\Software\Classes\*\shell]

[HKEY_CURRENT_USER\Software\Classes\*\shell\open_with_notepad_pp]
@="Open with Notepad++"
"icon"="C:\\portable\\npp.7.9\\notepad++.exe"

[HKEY_CURRENT_USER\Software\Classes\*\shell\open_with_notepad_pp\command]
@="\"C:\\portable\\npp.7.9\\notepad++.exe\" \"%1\""

UPDATE

Replace * with something like .svg and only for this extension the menu item will be shown.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSein KraftView Question on Stackoverflow
Solution 1 - Windows XpFactor MysticView Answer on Stackoverflow
Solution 2 - Windows Xpandromeda947View Answer on Stackoverflow
Solution 3 - Windows XpNick LegendView Answer on Stackoverflow