问题
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\""
I get an error
Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the item.
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\""
no error occurs. However, I would like to store AddRightClickFile.bat in %Appdata%\FindAlike.
Code in AddRightClickFile.bat is
reg add "HKEY_CURRENT_USER\Software\FindAlike" /f /v "TestFilePath" /t REG_SZ /d %1
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%?
EDIT
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);
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
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.
回答1:
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.
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 .
So, you have two options
Don't use the variable, use the full path.
Change the registry key type.
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\""
or, from a batch file
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: The only difference between both is the escaping of variable references
回答2:
I don't see any need to expand the variable at run time at all, so I would just enter the command as:
In a batch file:
@REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "\"%APPDATA%\FindAlike\AddRightClickFile.bat\" \"%%~1\"" /F>NUL
From the Command Prompt:
REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "\"%APPDATA%\FindAlike\AddRightClickFile.bat\" \"%~1\"" /F>NUL
[EDIT]
If that's all you're putting inside your AddRightClickFile.bat
then you could just bypass that file completely and enter the information directly as the command to run:
From a batch file:
@REG ADD "HKCU\Software\Classes\*\shell\Similar Files\command" /VE /D "CMD /C \"REG ADD \"HKCU\Software\FindAlike\" /V \"TestFilePath\" /D \"\\\"%%L\\\"\" /F^>NUL\"" /F>NUL
来源:https://stackoverflow.com/questions/44613513/running-a-bat-file-located-in-appdata-from-context-menu