【问题标题】:NSIS CreateShortCut not display correct string in target field of short cutNSIS CreateShortCut 不在快捷方式的目标字段中显示正确的字符串
【发布时间】:2017-05-16 19:00:16
【问题描述】:

以下是 NSIS 安装程序的摘录。

我的问题是当我执行时:

CreateShortCut "$DESKTOP\PRESS_Survey_PS.lnk" $shortcutPath "" $INSTDIR\images\appIcons\Survey.ico

它使用变量 $shortcutPath 在快捷方式的目标字段中生成:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command &.\PRESS_Survey.ps1"

$shortcutPath 变量是使用以下 strcpy 构建的:

StrCpy $powerShellPath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "
StrCpy $shortcutPath $powerShellPath$\"&.\PRESS_Survey.ps1$\"

当我将 $shortcutPath 变量写入我的 ps1 文件时:

FileWrite $0 $powershellComment$shortcutPath$NewLine

它产生这个:

#C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "&.\PRESS_Survey.ps1"

当我尝试使用以下命令执行快捷方式时:

"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command &.\PRESS_Survey.ps1"

它失败了,询问 PRESS_Survey.ps1 的位置。

当我在目标字段中将 CreateShortCut 版本替换为 FileWrite 版本时:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command "&.\PRESS_Survey.ps1"

它运行。

为什么在两个实例中 $shortcutPath 的值不一样? 这是我做错了什么还是Windows快捷方式问题? 无论哪种情况,有没有办法解决这个问题?

提前致谢。

【问题讨论】:

    标签: powershell nsis


    【解决方案1】:

    这里可能发生了两件事。

    首先,快捷方式目标和参数是两个不同的参数,所以你需要两个变量:

    StrCpy $powerShellPath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
    StrCpy $parameters '-command "&.\PRESS_Survey.ps1"'
    CreateShortcut "$DESKTOP\PRESS_Survey_PS.lnk" $powerShellPath $parameters "$INSTDIR\images\appIcons\Survey.ico"
    

    另一个问题是路径中的.\,. 表示当前目录,我不建议在快捷方式中使用它。如果您坚持,那么您需要记住,存储在快捷方式中的当前/工作目录来自 NSIS 中使用 SetOutPath 设置的路径,因此您可能需要执行以下操作:

    Push $OUTDIR ; Save the old path
    StrCpy $powerShellPath "$SysDir\WindowsPowerShell\v1.0\powershell.exe"
    StrCpy $parameters '-command "&.\PRESS_Survey.ps1"'
    SetOutPath "c:\path\where\PRESS_Survey\is\located" ; For .\ paths
    CreateShortcut "$DESKTOP\PRESS_Survey_PS.lnk" $powerShellPath $parameters "$INSTDIR\images\appIcons\Survey.ico"
    Pop $OUTDIR 
    SetOutPath $OUTDIR ; Restore
    

    【讨论】:

    • 谢谢。那成功了。关于:“另一个问题是路径中的 .\ , . 表示当前目录,我真的不建议在快捷方式中使用它。”我们将快捷方式的“开始于”字段设置为我们安装到的目录。
    • ...但是您知道安装程序中的完整路径是什么,因此您最好使用完整路径。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2011-03-05
    • 2012-11-26
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多