我通过使用此 RE_SZ 键添加新的注册表项来创建上下文菜单的快捷方式:cmd.exe start /min /c echo %1|clip
这使我能够复制右键单击文件的路径。
但是:它总是会短暂打开一个 cmd 窗口。
我怎样才能隐藏这个 cmd 窗口?
答案1
隐藏cmd
窗口使用ShellExecute 方法. 下一个注册表设置适用于单个文件:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\MyCopyAsPath]
[HKEY_CLASSES_ROOT\*\shell\MyCopyAsPath\command]
@="wscript D:\\VB_scripts\\SU\\1007076.vbs \"%1\""
内容D:\VB_scripts\SU\1007076.vbs
如下:
option explicit
On Error GoTo 0
If WScript.Arguments.Count = 1 Then
Dim objShell
Set objShell = CreateObject("shell.application")
objShell.ShellExecute "cmd.exe" _
, "/C echo(" & WScript.Arguments(0) & "|clip", "", "open", 0
Set objShell = nothing
Else
MsgBox "wrong numer of parameters"
End If
Wscript.Quit
上述脚本返回单个文件的完整路径(或目标文件快捷方式)并添加CRLF
(回车和换行)。您可以省略CRLF
使用set /P
技巧,如下所示:
objShell.ShellExecute "cmd.exe" _
, "/C <NUL set /P =""" & WScript.Arguments(0) & """|clip", "", "open", 0
下一步改进是用一对双引号将路径括起来"
:
objShell.ShellExecute "cmd.exe" _
, "/C <NUL set /P =""""" & WScript.Arguments(0) & """""|clip", "", "open", 0
仅供参考,这是存储上下文菜单的注册表项Copy as Path
:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\CopyAsPathMenu]
"ProgrammaticAccessOnly"="Apartment"
[HKEY_CLASSES_ROOT\*\shell\CopyAsPathMenu\DropTarget]
"CLSID"="{f3d06e7c-1e45-4a26-847e-f9fcdee59be0}"