如何创建具有相对目标的快捷方式 (.lnk)?
我的钥匙盘上有一个可执行文件,位于 dir\program\prog.exe 我想要 DoK 根目录上的可执行文件的快捷方式,即 prog.lnk 引用 dir\program\prog.exe。
但是,prog.lnk 似乎不能有相对目标。当 DoK 分配有不同的驱动器号(具体取决于它所连接的 PC)时,就会出现问题。
除了将 prog.exe 放在根目录中这一显而易见的建议之外,还有什么建议吗?
(最终,我想在安装时使用 nsis 执行此操作)
谢谢,
Rony
I have an executable on my disk-on-key in dir\program\prog.exe
I'd like to have a shortcut to the executable on the DoK's root directory, that is, prog.lnk would refer to dir\program\prog.exe.
However, it seems that prog.lnk can't have a relative target. This is a problem when the DoK will have different drive letters assigned to it, depending on which PC it's connected to.
Any suggestions, aside from the obvious one of putting prog.exe in the root dir?
(ultimately, I'd like to do this at install time using nsis)
Thanks,
Rony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我们假设 cmd.exe 对于所有 Windows 安装都位于相同的绝对路径上(可能,但并非万无一失),您可以找出 .lnk 文件来启动 cmd,如下所示
/d 将目录设置为 .lnk 文件的目录。 lnk 文件
启动命令可能还有其他有用的选项(例如 /b)
If we assume cmd.exe would be on the same absolute path for all windows installations (probable, but not fool-proof) you can make out the .lnk file to start cmd like this
/d sets the directory to the directory of the .lnk file
There might be other useful options for the start command (e.g. /b)
虽然快捷方式可以包含目标的相对路径(.lnk 文件有一个名为 SLDF_HAS_RELPATH) NSIS 不支持创建除“普通”快捷方式以外的任何内容,因此您需要直接写入二进制数据(.lnk 格式非常稳定,并且已由 MS 记录),
如下调用:
虽然生成的 .lnk 似乎可以工作,但我不确定是否会在生产代码中使用它
更好的解决方案是创建一个像 Oleg 建议的小 NSIS 应用程序(NSIS 应用程序可以在 .exe 末尾包含嵌入数据它可以在运行时从自身读取等......)
While it is possible for shortcuts to contain a relative path to the target (.lnk files have a flag called SLDF_HAS_RELPATH) NSIS does not support creating anything other than "normal" shortcuts so you need to write the binary data directly (The .lnk format is pretty stable and has been documented by MS)
Call it like this:
While the generated .lnk seems to work, I'm not sure if I would use this in production code
A much better solution is to create a little NSIS app like Oleg suggests (NSIS applications can contain embedded data at the end of the .exe that it can read from itself at runtime etc..)
安装“Relative”
我正在使用一些技巧。该方法如下屏幕截图所示:
它启动 explorer.exe,然后传递一个相对路径像这样:
我为此使用名为“Relative”的小工具。安装后,您可以右键单击文件,然后选择
创建相对快捷方式...
。然后它会弹出一个另存为...
框。这不像简单的拖放那么舒服,但它有帮助。 (它还为它创建的链接使用自己的特殊图标。因此链接中不再有原始图标。请参见上文。您可能喜欢也可能不喜欢这个。)Install "Relative"
I'm using a bit of a hack. The approach is shown in this screenshot:
It starts explorer.exe and then passes a relative path like so:
I'm using a small tool called "Relative" for this. After you install it, you can right-click a file and then select
Create relative shortcut...
. It will then pop up aSave as...
box. This is not quite as comfortable as simply dragging and dropping but it helps. (It also uses its own special icon for the links it creates. So you no longer have the original icon in the link. See above. You may or may not like this.)你是对的。您不能在可移动媒体上使用快捷方式(.lnk),因为就像您自己编写的那样,可移动媒体在不同情况下可以有不同的驱动器号。
如果您需要在驱动器的根目录中保存某些内容,请使用 CMD、VBS 或 JS 而不是快捷方式。在某些情况下,人们还使用标准 EXE,它使用 INI 文件等配置文件从另一个子目录启动另一个程序。 CD/DVD的典型做法是在插入盘后启动安装程序。也许这种方式也能满足您的要求?
You are right. You can not use shortcuts (.lnk) on a removable media, because like you wrote yourself the removable media can have different drive letters in different situations.
If you need to have something in a root directory of the drive one use a CMD, VBS or JS instead of a shortcut. In some situation one use also a standard EXE which use a config file like INI file to start another program from the other subdirectory. It is a typical practice for CD/DVD to implement starting of a setup program after inserting of the disk. Probably this way will be OK in for your requirements also?
我相信 NT 4 资源工具包命令 SHORTCUT.exe 将创建相对链接的快捷方式。我希望 Microsoft 能够创建一个新的受支持实用程序或 Powershell Cmdlet,以方便创建相关 .lnk 文件或使 NTFS 链接更像 Linux 符号链接。在此之前,我使用 .cmd/.bat 和 .ps1 文件来实现此目的。
Program.cmd
Program.ps1
不幸的是,虽然 .cmd/.bat 文件可以从任何上下文运行(运行对话框、CMD 提示符、Powershell、双击文件资源管理器等),但它们无法调用存储的内容在 UNC 路径上。为了在 Powershell 中公开 UNC 路径中的内容(我为 Git 和 Mercurial 等工具执行此操作),我将创建上述脚本的 Powershell 版本。
I believe that the NT 4 Resource Kit command SHORTCUT.exe would create relative linked shortcuts. I wish that Microsoft would create a new supported utility or Powershell Cmdlet to facilitate the creation of relative .lnk files or make NTFS links work more like Linux symbolic links. Until then, I use .cmd/.bat and .ps1 files for this purpose.
Program.cmd
Program.ps1
Unfortunately, though .cmd/.bat files will run from any context (the run dialog, a CMD prompt, in Powershell, double clicking in File Explorer, etc), they cannot call things stored on a UNC path. For exposing things in a UNC path, in Powershell (I do this for tools like Git and Mercurial), I will create a Powershell version of the above script.