如何将新项目添加到 Windows 中文件夹和文件的右键单击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1821662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:31:10  来源:igfitidea点击:

How to add new items to right-click event on Folders and Files in Windows?

windowseventsregistry

提问by Chicago

I did google couple of tutorials on google.

我在谷歌上做了几个谷歌教程。

I am able to add right-click menu item to a FOLDERby doing this:

通过执行以下操作,我可以将右键单击菜单项添加到文件夹中

[HKEY_CLASSES_ROOT\Directory\shell\Command]
@="TestRightClick:"

[HKEY_CLASSES_ROOT\Directory\shell\Command\Command]
@="myExecutable.exe %L"

I need to add this to a FILE as well.

我也需要将它添加到 FILE 中。

1) Where do I add it in the registry?

1)我在哪里添加它在注册表中?

2) And how do I pass parameters to my executable in case if I am selecting multiple files?

2) 如果我选择多个文件,我该如何将参数传递给我的可执行文件?

Related:

有关的:

How to pass in multiple file/folder paths via a rigth-click event(verb) to an executable?

如何通过 rigth-click 事件(动词)将多个文件/文件夹路径传递给可执行文件?

采纳答案by Jason R. Coombs

  1. Files have context menus by extension. Add your Command registry keys to the appropriate extension or HKEY_CLASSES_ROOT\* to affect all files.
  2. You can use %1 to pass the filename to the application (much like you've indicated with %L above). If you select multiple files, each will be called separately, as if you right-clicked each one individually.
  1. 文件按扩展名具有上下文菜单。将您的 Command 注册表项添加到适当的扩展名或 HKEY_CLASSES_ROOT\* 以影响所有文件。
  2. 您可以使用 %1 将文件名传递给应用程序(就像您在上面用 %L 指示的那样)。如果您选择多个文件,每个文件将被单独调用,就像您分别右键单击每个文件一样。

I'm not aware of any easy way to pass multiple items from a right-click context menu to one executable instance.

我不知道有什么简单的方法可以将多个项目从右键单击上下文菜单传递到一个可执行实例。

回答by zenden2k

You can do it with my program singleinstance. No shell extensions involved.

你可以用我的程序singleinstance做到这一点。不涉及外壳扩展。

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\.txt\Shell\p4merge]
"MultiSelectModel"="Player"

[HKEY_CLASSES_ROOT\SystemFileAssociations\.txt\Shell\p4merge\Command]
@="\"d:\singleinstance.exe\" %1 \"C:\Program Files\Perforce\p4merge.exe\" $files --si-timeout 400"

回答by Factor Mystic

The key word you're looking for is 'verbs' or 'handlers' not 'events'.

您要查找的关键词是“动词”或“处理程序”,而不是“事件”。

Context menu verbs for particular file extensions can be placed under the ProgID for the file type, the Perceived Type key (if the file type has a perceived type), the AllFileSystemObjects key, or the Base Class Key (*).

特定文件扩展名的上下文菜单动词可以放置在文件类型的 ProgID、感知类型键(如果文件类型具有感知类型)、AllFileSystemObjects 键或基类键 (*) 下。

Note that writing to these keys in the HKEY_CLASSES_ROOThive will redirect the writes to HKEY_LOCAL_MACHINE\Software\Classes, and will require elevated privileges. If you write to the HKEY_CURRENT_USER\Software\Classestree, you can do this with standard user rights.

请注意,写入HKEY_CLASSES_ROOT配置单元中的这些键会将写入重定向到HKEY_LOCAL_MACHINE\Software\Classes,并且需要提升权限。如果您写入HKEY_CURRENT_USER\Software\Classes树,则可以使用标准用户权限执行此操作。

It's up to you to handle a scenario where multiple files are selected. One instance of your application will be launched per file you have selected. You can solve this by checking if another instance of your application is running, and using Inter-Process Communication to notify the existing instance that other extensions have been selected.

由您来处理选择多个文件的场景。您选择的每个文件都会启动一个应用程序实例。您可以通过检查应用程序的另一个实例是否正在运行并使用进程间通信通知现有实例已选择其他扩展来解决此问题。

On MSDN, be sure to read

在 MSDN 上,请务必阅读

回答by Beej

See GitHub SingleInstanceAccumulatorfor a C# implementation of the well worn Mutex + COPYDATA approach to this.

请参阅GitHub SingleInstanceAccumulator以了解磨损的 Mutex + COPYDATA 方法的 C# 实现。

other stack-o'sexpressing the need.

其他 stack-o表示需要。

Explorer Context Menu config

资源管理器上下文菜单配置

::creates the entry
:: and crucial multi-file handling property
reg add "HKEY_CLASSES_ROOT\FileType\shell\YourNewContextMenu" /f /v "MultiSelectModel" /d "Player"

::your desired command line
reg add "HKEY_CLASSES_ROOT\FileType\shell\YourNewContextMenu\command" /f /ve /t REG_EXPAND_SZ /d "***see command line examples***"

e.g. On my system, for ".mov" files, I would replace FileTypeabove with VLC.mov

例如,在我的系统上,对于“.mov”文件,我会将FileType上面替换为VLC.mov

Complex REG ADD example

复杂的 REG ADD 示例

Replace "* see command line examples *" above with your desired command line.
Note: quotes& environment variablesmust be escaped and escaping work slightly differently for the initial command versus later in the string!?!

将上面的“ * see command line examples *”替换为您想要的命令行。
注意:必须对引号环境变量进行转义,并且初始命令与字符串中的后面的转义工作略有不同!?!

λ reg add "HKEY_CLASSES_ROOT\VLC.mov\shell\Transcode\command" /f /ve /t REG_EXPAND_SZ /d "\"^%bin^%\SingleInstanceAccumulator\" -f \"-c:powershell -ExecutionPolicy bypass "\"^%bin^%\test.ps1\"" -list '$files'\" \"%1\""

SingleInstanceAccumulator.exe Usage

SingleInstanceAccumulator.exe 用法

"-c:command line" (default: cmd /c echo $files && pause)
  $files will be replace with aggregated list

-f = output each item on separate line to new tempfile
  $files will be replaced by the tempfile path
  quote will default to nothing

-d:delimiter (default: ,)
-q:quote (default: ")
-t:timeout millisecs (default: 200)
-w = hidden launch
-v = debug output

Command Line Examples

命令行示例

note: initial command must have pathfor shell > command to work

注意:初始命令必须具有shell > 命令才能工作的路径

PowerShell & temp file

PowerShell 和临时文件

note: -fusage

注意:-f用法

"%bin%\SingleInstanceAccumulator" -f "-c:powershell -ExecutionPolicy bypass "%bin%\test.ps1" -list '$files'" "%1"

PowerShell & inlinefiles list

PowerShell 和内联文件列表

note: -qusage

注意:-q用法

"%bin%\SingleInstanceAccumulator" -q:' "-c:powershell -ExecutionPolicy bypass "%bin%\test.ps1" -list $files" "%1"

test.ps1 (with temp file)

test.ps1(带临时文件)

powershell
param(
  [String]$listFilePath
)

gc $listFilePath | % { $_ }

pause

erase $listFilePath

pause

test.ps1 (with files array parm)

test.ps1(带有文件数组参数)

param(
  [String[]]$filesList
)

$filesList | % { $_ }

pause