是否可以在以管理员身份运行的 Windows 中创建相对路径的快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18756671/
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
Is it possible to make a shortcut to a relative path in Windows that runs as admin?
提问by Trevor Merrifield
To create a shortcut with a relative path, set the target to the following.
要使用相对路径创建快捷方式,请将目标设置为以下内容。
%windir%\system32\cmd.exe /c start your_relpath\app.exe [your_args]
It is also straightforward to make the program run as administrator.
让程序以管理员身份运行也很简单。
Properties > Advanced > Run as administrator
But putting the two together doesn't work. When cmd.exe
is started as admin, it loses the current working directory and goes to %windir%\system32
. The relative path starts there which is not intended.
但是把两者放在一起是行不通的。当cmd.exe
以管理员身份启动时,它会丢失当前工作目录并转到%windir%\system32
. 相对路径从那里开始,这不是故意的。
Is there any way to make a shortcut that is bothrelative and run as admin?
有什么办法可以创建一个既相对又以管理员身份运行的快捷方式?
采纳答案by Trevor Merrifield
I didn't find an easy way but that's fine. I was concerned that shortcuts with absolute paths would break between computers. I didn't realize that Windows corrected them automatically. This seems sufficient, though relative paths would have been more elegant.
我没有找到简单的方法,但这很好。我担心具有绝对路径的快捷方式会在计算机之间中断。我没有意识到 Windows 会自动更正它们。这似乎足够了,尽管相对路径会更优雅。
Although shortcuts, when created, point to specific files or folders, they may break if the target is moved to another location. Microsoft Windows has standard algorithms for fixing up shortcuts when they are moved. Windows 9x-based versions of Windows use a simple search algorithm to fix broken shortcuts.[1] On Windows NT-based operating systems and the NTFS file system, the target object's unique identifier is stored in the shortcut file and Windows can use the Distributed Link Tracking service for tracking the targets of shortcuts, so that the shortcut may be silently updated if the target moves to another hard drive.[4]
尽管快捷方式在创建时指向特定文件或文件夹,但如果将目标移动到其他位置,它们可能会中断。Microsoft Windows 具有用于在移动快捷方式时修复快捷方式的标准算法。基于 Windows 9x 的 Windows 版本使用简单的搜索算法来修复损坏的快捷方式。 [1] 在基于 Windows NT 的操作系统和 NTFS 文件系统上,目标对象的唯一标识符存储在快捷方式文件中,Windows 可以使用分布式链接跟踪服务来跟踪快捷方式的目标,以便在出现以下情况时可以静默更新快捷方式目标移动到另一个硬盘驱动器。 [4]
http://en.wikipedia.org/wiki/File_shortcut#Microsoft_Windows
http://en.wikipedia.org/wiki/File_shortcut#Microsoft_Windows
回答by semtex41
For a portable shortcut, that works with a relative path, use the following:
对于使用相对路径的便携式快捷方式,请使用以下命令:
Simply drop this in the "Target" of the Shortcut Properties:
只需将其放在快捷方式属性的“目标”中:
%COMSPEC% /C "start GoogleChromePortable.exe -enableextensions -incognito"
Expected behavior: A brief CMD window will popup and close just prior to chrome launching.
预期行为:一个简短的 CMD 窗口将在 Chrome 启动之前弹出并关闭。
Note: the "Start in:" option can be left blank. Also note, the .exe is located in the same folder as the shortcut, but move the folder and the shortcut works just fine.
注意:“开始于:”选项可以留空。另请注意,.exe 与快捷方式位于同一文件夹中,但移动文件夹,快捷方式工作正常。
This is confirmed to work in Win10 1607.
这已确认在 Win10 1607 中有效。
Credit: some other post on stackoverflow a while ago.
信用:前一段时间关于stackoverflow的其他一些帖子。
回答by aoedipus
Was having trouble with auto-close window behavior. Just got it working with the following:
自动关闭窗口行为有问题。刚刚得到它与以下工作:
%COMSPEC% /c "start theRelativePath" | taskkill /F /IM cmd.exe
%COMSPEC% /c "start theRelativePath" | taskkill /F /IM cmd.exe