NSIS-将EXE版本放入安装程序的名称中

NSIS-将EXE版本放入安装程序的名称中,nsis,Nsis,具有在脚本中定义的名称变量: Name "MyApp" 它定义了安装程序的名称,显示为窗口标题等 有没有办法将.NET版本号从我的主EXE中提取出来并附加到名称中 因此,我的安装程序名称将自动成为“MyApp V2.2.0.0”或其他名称?可能有一种非常简单的方法来实现这一点,但我不知道它是什么。当我第一次开始使用NSIS时,我开发了此解决方法以满足我的需要,此后再也没有重新考虑过这个问题,看看是否还有更优雅的方法 我希望我的安装程序与我的主可执行文件具有相同的版本号、说明和版权信息。因此,我

具有在脚本中定义的名称变量:

Name "MyApp"
它定义了安装程序的名称,显示为窗口标题等

有没有办法将.NET版本号从我的主EXE中提取出来并附加到名称中


因此,我的安装程序名称将自动成为“MyApp V2.2.0.0”或其他名称?

可能有一种非常简单的方法来实现这一点,但我不知道它是什么。当我第一次开始使用NSIS时,我开发了此解决方法以满足我的需要,此后再也没有重新考虑过这个问题,看看是否还有更优雅的方法

我希望我的安装程序与我的主可执行文件具有相同的版本号、说明和版权信息。因此,我编写了一个名为GetAssemblyInfoForNSIS的短C#应用程序,它从可执行文件中提取该文件信息,并将其写入安装程序包含的.nsh文件中

以下是C#应用程序:

在NSIS脚本的顶部,我执行以下操作:

!define GetAssemblyInfoForNSIS "C:\MyPath\GetAssemblyInfoForNSIS.exe"
!define PrimaryAssembly "C:\MyPath\MyApp.exe"
!define VersionHeader "C:\MyPath\MyAppVersionInfo.nsh"
!system '"${GetAssemblyInfoForNSIS}" "${PrimaryAssembly}" "${VersionHeader}"'
!include /NONFATAL "${VersionHeader}"

!ifdef VERSION
    Name "My App ${VERSION}"
!else
    Name "My App"
!endif

!ifdef DESCRIPTION
    VIAddVersionKey FileDescription "${DESCRIPTION}"
!endif

!ifdef COPYRIGHT
    VIAddVersionKey LegalCopyright "${COPYRIGHT}"
!endif

前3个定义设置调用GetAssemblyInfoForNSIS.exe时使用的文件名。此系统调用发生在安装程序的编译过程中,并在包含.nsh文件之前生成该文件。我使用/NONFATAL开关,以便在生成包含文件时出错时,我的安装程序不会完全失败。

您可以在没有.NET的情况下使用,但遵循相同的基本逻辑:

以下是ExtractVersionInfo.nsi:

!define File "...\path\to\your\app.exe"

OutFile "ExtractVersionInfo.exe"
SilentInstall silent
RequestExecutionLevel user

Section

 ## Get file version
 GetDllVersion "${File}" $R0 $R1
  IntOp $R2 $R0 / 0x00010000
  IntOp $R3 $R0 & 0x0000FFFF
  IntOp $R4 $R1 / 0x00010000
  IntOp $R5 $R1 & 0x0000FFFF
  StrCpy $R1 "$R2.$R3.$R4.$R5"

 ## Write it to a !define for use in main script
 FileOpen $R0 "$EXEDIR\App-Version.txt" w
  FileWrite $R0 '!define Version "$R1"'
 FileClose $R0

SectionEnd
编译一次,然后从真正的安装程序调用它:

; We want to stamp the version of the installer into its exe name.
; We will get the version number from the app itself.
!system "ExtractVersionInfo.exe"
!include "App-Version.txt"
Name "My App, Version ${Version}"
OutFile "MyApp-${Version}.exe"

我在NSIS wiki上找到了这样做的方法:


您可以使用MSBuild实现这一点

  • 只需将
    .nsi
    脚本添加到project并设置此文件属性
    Copy to Output Directory
    value
    Copy always
    Copy if newer

  • 将以下代码添加到项目文件(例如
    .csproj
    或.vbproj)中(假设
    nsi
    脚本的名称为
    installer.nsi


  • NSIS编译后调用简单VBS脚本:

    Set ddr = CreateObject("Scripting.FileSystemObject")
    Version = ddr.GetFileVersion( "..\path_to_version.exe" )
    ddr.MoveFile "OutputSetup.exe", "OutputSetup_" & Version & ".exe"
    

    由于NSISv3.0,无需使用任何第三方软件即可完成此操作:

    !getdllversion "MyApp.exe" ver
    Name "MyName ${ver1}.${ver2}.${ver3}.${ver4}"
    OutFile "my_name_install_v.${ver1}.${ver2}.${ver3}.${ver4}.exe"
    

    由于NSIS v3.0a0可以直接在脚本中执行,因此不需要外部工具:

    示例代码(来自文档):


    这就是我所说的解决方案!谢谢:)我想知道:为什么需要编译exe而不仅仅是包含一些代码?这是一个非常有趣的问题。我喜欢NSIS的新v3.x如何通过,!GetDlVersion和VIProductVersion/ViadVersionKey来实现这一点。不过,如果有人在v3.x上,这是很有趣的。没人会说关于这一点,但我认为不使用插件解决它的最好方法是使用函数“searchparse”,因为NSIS>V2.39我写了一篇关于它的小文章。先生,你是我的英雄。我使用了你的方法,效果非常好:“```!GetdLversion”。\${app\u name}\${app\u name}.exe“ver!定义产品版本”${ver1}.${ver2}.${ver3}.{ver4}" !定义版本“${ver1}.${ver2}.${ver3}.${ver4}”viadVersionKey“ProductName”${app_VERSION}”viadVersionKey“FileVersion”${PRODUCT_VERSION}”viadVersionKey“FileVersion”${PRODUCT VERSION}”viadVersionKey“VIProductVersion”${VERSION}”viadVersionKey“Legalcyright”;(C)${author}“viadVersionKey”viadVersionKey“FileDescription”${app app name}”```
    <Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
      <!-- Getting assembly information -->
      <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
        <Output TaskParameter="Assemblies" ItemName="myAssemblyInfo"/>
      </GetAssemblyIdentity>
      <!-- Compile NSIS installer script to get installer file -->
      <Exec Command='"%programfiles(x86)%\nsis\makensis.exe" /DVersion=%(myAssemblyInfo.Version) "$(TargetDir)installer.nsi"'>
        <!-- Just to show output from nsis to VS Output -->
        <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
      </Exec>
    </Target>
    
    # define installer name
    OutFile "MyApp-${Version}.exe"
    
    Set ddr = CreateObject("Scripting.FileSystemObject")
    Version = ddr.GetFileVersion( "..\path_to_version.exe" )
    ddr.MoveFile "OutputSetup.exe", "OutputSetup_" & Version & ".exe"
    
    !getdllversion "MyApp.exe" ver
    Name "MyName ${ver1}.${ver2}.${ver3}.${ver4}"
    OutFile "my_name_install_v.${ver1}.${ver2}.${ver3}.${ver4}.exe"
    
    !getdllversion "$%WINDIR%\Explorer.exe" Expv_
    !echo "Explorer.exe version is ${Expv_1}.${Expv_2}.${Expv_3}.${Expv_4}"