How to add an entry in the Windows context menu for files with a specific extension?

你说的曾经没有我的故事 提交于 2019-12-11 08:39:32

问题


I know that many questions are asked about how customizing the shell context menu, but what I've tried yet doesn't work so I'm adding a new question.

I'd like to add an entry "Open with Log Viewer" in the context menu when right-clicking on files with ".log" extension, to not change the default application associated with .log files (notepad) but allow the user to choose a custom application to open them.

To do this, I opened the registry key HKEY_CLASSES_ROOT\.log, and added some keys shell\OpenWithLogViewer\command with the correct values, but the entry is not displayed when I right-click on a file with .log extension.

Would you know how to fix this?

The key HKEY_CLASSES_ROOT\.log has for default value txtfile, and contains a subkey called PersistentHandler. Can this subkey be the origin of the problem?


回答1:


Add another registry key (e.g. HKEY_CLASSES_ROOT\logfile), create the shell structure below that key and change the default value of the .log key to logfile. One way to do this is by saving the following lines to a .reg file and merging that file into the registry.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\.log]
@="logfile"

[HKEY_CLASSES_ROOT\logfile]

[HKEY_CLASSES_ROOT\logfile\shell]
@="OpenWithLogViewer"
; make OpenWithLogViewer the default action

[HKEY_CLASSES_ROOT\logfile\shell\OpenWithLogViewer]
@="Open with &Log Viewer"
; set label and access key

[HKEY_CLASSES_ROOT\logfile\shell\OpenWithLogViewer\command]
@="\"C:\\path\\to\\logviewer.exe\" %1"


This separates the type (logfile) from the extension (.log). That way you can define the possible actions for a type in one place and associate arbitrary extensions with that type.

Note that you can also define this on a per-user basis by using HKEY_CURRENT_USER\Software\Classes instead of HKEY_CLASSES_ROOT. User entries take precedence over system entries. This is useful when you want to change file associations or add custom actions for your own user, but don't have admin privileges on the system.




回答2:


If you want to add a entry for a file extension you don't "own" and you never want to be the default action then you can use the SystemFileAssociations key:

[HKEY_CLASSES_ROOT\SystemFileAssociations\.log\shell\mycommand]
@="My Command"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.log\shell\mycommand\command]
@="\"c:\\path\\myapp.exe\" \"%1\""

To deal proactively with the consequences of a change to default programs, you can use HKEY_CLASSES_ROOT\SystemFileAssociations to register verbs and other association information. Due to their location after the ProgID in the association array, these registrations are lower priority. These SystemFileAssociationsregistrations are stable even when users change the default programs, and provide a location to register secondary verbs that will always be available for a particular file type.

This key is available on Windows XP and higher...



来源:https://stackoverflow.com/questions/10618977/how-to-add-an-entry-in-the-windows-context-menu-for-files-with-a-specific-extens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!