Add menu item to Windows context menu only for specific filetype
Asked Answered
P

4

59

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?

Phosphorite answered 23/1, 2010 at 16:12 Comment(0)
P
102
  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:

Polaris answered 23/1, 2010 at 19:26 Comment(10)
I am aware of this method to add entries to context menu. However, can you also mention how to make any entry as default? I know how to do this using "folder options", but command line or registry options would be great. (I think we need to do it with editflags dword entry, but not sure.)Accumulator
@Accumulator set the default value of the shell key to the name of the verb key you wish to be defaultPolaris
I have found a situation where regedit would remove the text I entered for the command value. This was fixed by using double backslash in stead of single. eg. "C:\\Program Files (x86)\My Program\\bin\\myprogram.exe" "%1"Preservative
Couldn't get it right with .vcxproj files - they simply appear differently in the registry. Any idea?Feud
If you want to add the context menu to all file types, skip step 1, and replace jpegfile with * in steps 2 thru 4Cutpurse
II can't get this to work. DO I still need to add the same thing under HKEY_CLASSES_ROOT also?Krenek
This is wrong -- when you have your own application, you do not add anything to another "ProgID", you create your own and associate it with the file type through the OpenWithProgIds key on the filetype key (e.g. .jpg under Software\Classes key under appropriate hive).Trela
@amn the original question isn't asking how to get an app to show as available to handle the file type exclusively, it's asking how to edit and existing file type's context menu with a new custom option. These are separate objectives.Polaris
Exactly -- an existing file type, not only that but a "public" (IANA registered, even) type at that -- JPEG. The developer wish to associate their application with said type. The key point being that the file type in question isn't something only their application is [supposed to be] associated with -- chances are, half a dozen JPEG applications are installed on a given host. Meaning that naively writing registry keys like "open" or "edit" in the already existing "jpegfile" key, is a potentially destructive operation -- you may be overwriting an existing association. That's what I meant.Trela
i don't think this works on windows 10; at least i couldn't get it working. i had to put it in SystemFileAssociations. all the stuff in *\Classes (even when it appears in the HKCR\.whatever view) seems to be completely ignored if it's in SystemFileAssociations. (i found somebody else who made the same discovery here #39551712)Horizontal
U
55

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://learn.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.

Untried answered 11/12, 2017 at 2:58 Comment(5)
Windows 10 - December 2019: I'm giving my vote to this answer. I tried the accepted answer, but it simply did not work and I don't understand why. I can only presume it used to work, but no longer does in newer versions of windows or there are some system security caveats that make it work / or not. My version of windows in vanilla, not locked down, etc. Just putting this note in for anyone who comes across it and accepted answer doesnt work.Krenek
@SteveCarter it's typically the opposite: writing to HKCR requires extra permissions, whereas writing to HKCU is a user operation without extra permissionsPolaris
@FactorMystic thanks for your feedback. Can you offer any info as to why the accepted answer might not work? I spent hours looking at this. I'm the first to admit that I am a novice when it comes to the windows registry, but I followed what you have written to the letter, but I do not get the right-click working to run my program. Is there anything else that I might be missing? Appreciate any feedback you could offer. Happy to ask another question if needs be, but its fundamentally the same issue.Krenek
FWIW I just fought this in Windows 10 for .SLN files Windows 10 - 1909 (June 2020) watch out for the override in `HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts` (described here superuser.com/questions/1424724/…) this will override the ability to add an additional option to the context menu UNTIL you delete it.Coquito
This is the one which worked for me. Thanks!Epact
L
3
  1. In the registry (via regedit.exe) navigate to:

HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\Shell

  1. Create a new Key with the name "MyApp".

  2. Create another key within MyApp and name it "command".

  3. Within command, change the (Default) value to be your app path, like this:

"D:\Python_Programlar\pdftopng\app.exe" "%1"

  1. In MyApp, enter a text in the (Default) value to be what do you want to see in the Context Menu.

  2. Optional: In MyApp, add a new String Value or Expandable String Value and set this to be the application icon (.ico) file path (e.g. "D:\Python_Programlar\pdftopng\app.ico") to see an icon next to your new entry in the Windows Explorer context menu:

screen snippet of Windows Explorer context menu

Leatherman answered 4/10, 2023 at 7:59 Comment(0)
H
0

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.

Hoagland answered 25/10, 2020 at 11:55 Comment(1)
this adds an item for all filetypes, not a specific one.Callboy

© 2022 - 2024 — McMap. All rights reserved.