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

问题描述 投票:16回答:7

NSIS有一个您在脚本中定义的Name变量:

Name "MyApp"

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

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

这样我的安装程序名称将自动为“MyApp V2.2.0.0”或其他什么?

nsis
7个回答
22
投票

可能有一种非常简单的方法可以做到这一点,但我不知道它是什么。当我第一次开始使用NSIS时,我开发了这种解决方法以满足我的需求,并且没有重新考虑问题,因为看看是否有更优雅的东西。

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

这是C#应用程序:

using System;
using System.Collections.Generic;
using System.Text;

namespace GetAssemblyInfoForNSIS {
    class Program {
        /// <summary>
        /// This program is used at compile-time by the NSIS Install Scripts.
        /// It copies the file properties of an assembly and writes that info a
        /// header file that the scripts use to make the installer match the program
        /// </summary>
        static void Main(string[] args) {
            try {
                String inputFile = args[0];
                String outputFile = args[1];
                System.Diagnostics.FileVersionInfo fileInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(inputFile);
                using (System.IO.TextWriter writer = new System.IO.StreamWriter(outputFile, false, Encoding.Default)) {
                    writer.WriteLine("!define VERSION \"" + fileInfo.ProductVersion + "\"");
                    writer.WriteLine("!define DESCRIPTION \"" + fileInfo.FileDescription + "\"");
                    writer.WriteLine("!define COPYRIGHT \"" + fileInfo.LegalCopyright + "\"");
                    writer.Close();
                }
            } catch (Exception e) {
                Console.WriteLine(e.Message + "\n\n");
                Console.WriteLine("Usage: GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh\n");
            }
        }
    }
}

所以,如果你像这样使用那个应用程序:

GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh

你会得到一个名为MyAppVersionInfo.nsh的文件看起来像这样(假设这个信息在你的可执行文件中):

!define VERSION "2.0" 
!define DESCRIPTION "My awesome application"
!define COPYRIGHT "Copyright © Me 2010"

在我的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个定义了在!system调用GetAssemblyInfoForNSIS.exe时使用的文件名。此系统调用在安装程序编译期间进行,并在包含之前生成.nsh文件。我使用/ NONFATAL开关,以便在生成包含文件时发生错误,我的安装程序不会完全失败。


11
投票

您可以使用GetVersion plugin在没有.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"

3
投票

3
投票

从NSISv3.0开始,这可以使用!getddlversion完成,而无需使用任何第三方软件:

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

1
投票

您可以使用MSBuild实现此目的。

  1. 只需将您的.nsi脚本添加到项目并设置此文件属性Copy to Output DirectoryCopy alwaysCopy if newer
  2. 按照代码添加到您的项目文件(例如.csproj或.vbproj)(假设您的nsi脚本名称为installer.nsi<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>
  3. $Version脚本中使用nsi变量: # define installer name OutFile "MyApp-${Version}.exe"

0
投票

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

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

-1
投票

从NSIS v3.0a0开始,您可以直接在脚本中完成,无需外部工具:!getdllversion

示例代码(来自文档):

!getdllversion "$%WINDIR%\Explorer.exe" Expv_
!echo "Explorer.exe version is ${Expv_1}.${Expv_2}.${Expv_3}.${Expv_4}"
© www.soinside.com 2019 - 2024. All rights reserved.