从上下文菜单运行位于%AppData%中的.bat文件 [英] Running a .bat file located in %AppData% from context menu

查看:138
本文介绍了从上下文菜单运行位于%AppData%中的.bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用以下注册表项将条目添加到Windows资源管理器上下文菜单中时:

When I add an entry to the Windows Explorer context menu using the registry entries as follows:

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command]
 @="%AppData%\\FindAlike\\AddRightClickFile.bat  \"%1\""

我得到一个错误

Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the item.

如果我将文件AddRightClickFile.bat复制到C:\ Windows \ System32并将注册表项更改为

If I copy the file AddRightClickFile.bat to C:\Windows\System32 and change the registry entries to

[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files]
[HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command]
@="AddRightClickFile.bat  \"%1\""

没有错误发生.但是,我想将AddRightClickFile.bat存储在%Appdata%\ FindAlike中.

no error occurs. However, I would like to store AddRightClickFile.bat in %Appdata%\FindAlike.

AddRightClickFile.bat中的代码为

Code in AddRightClickFile.bat is

reg add  "HKEY_CURRENT_USER\Software\FindAlike"  /f /v "TestFilePath" /t REG_SZ /d  %1

有什么方法可以将上下文文件中的.bat文件存储在%AppData%的子文件夹中?

Is there any way I can get the .bat file to run from the context menu command while storing it in a subfolder of %AppData%?

编辑

我已经使用以下代码在代码中创建了注册表项:

I have created the Registry key in code using the following code:

RegistryKey rk = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Classes\*\shell\Similar Files\command");
        string sValue = @"%AppData%\FindAlike\AddRightClickFile.bat ""%1""";
        rk.SetValue("",sValue, RegistryValueKind.ExpandString);

并按如下所示设置AddRightClickFile.bat:

and set the AddRightClickFile.bat as follows:

start  
reg add  "HKEY_CURRENT_USER\Software\FindAlike"  /f /v "RightClickFileName"       /t REG_SZ /d  %1 
exit 0

这项工作正常,但是在我右键单击的文件目录中创建了一个命令窗口.通过任务管理器查看进程,我看到已创建cmd.exe和conhost.exe进程.如果删除命令窗口,则两个过程均消失.我有什么办法可以自动终止这些进程而不必按名称杀死它们,这可能会带来不良后果.

This work OK but creates a command window in the directory of the file I right-click on. Looking at processes with Task manager I see that cmd.exe and conhost.exe processes are created. If I delete the command window both processes disappear. Is there any way I can terminate these processes automatically without killing them by name, which might have undesired consequences.

推荐答案

您的主要问题是%AppData%被保存为变量引用,但是当读取注册表值时,该变量不会扩展为其值.

Your main problem is that %AppData% is saved as a variable reference but when the registry value is read, the variable is not expanded to its value.

原因是注册表项的(default)值具有REG_SZ类型.如果要存储变量引用并自动扩展其值,则需要注册表键为REG_EXPAND_SZ类型.

The reason is that the (default) value for a registry key has REG_SZ type. If you want to store a variable reference and automatically expand its value you need the registry key to be of REG_EXPAND_SZ type .

所以,您有两个选择

  1. 不要使用变量,请使用完整路径.

  1. Don't use the variable, use the full path.

更改注册表项类型.

第一个选项没有任何问题,但是第二个选项不能通过regedit完成.在命令行中,您可以使用

First option has not any problem, but second one can not be done from regedit. From command line you can use

reg.exe add "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command" /f /ve /t REG_EXPAND_SZ /d "\"%^AppData%\FindAlike\AddRightClickFile.bat\" \"%1\""

或从批处理文件

    reg.exe add "HKEY_CURRENT_USER\SOFTWARE\Classes\*\shell\Similar Files\command" ^
        /f /ve /t REG_EXPAND_SZ ^
        /d "\"%%AppData%%\FindAlike\AddRightClickFile.bat\" \"%%1\""

note :两者之间的唯一区别是变量引用的转义

note: The only difference between both is the escaping of variable references

这篇关于从上下文菜单运行位于%AppData%中的.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆