gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 14:34:26 28 4
gpt4 key购买 nike

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

Name "MyApp"


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

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

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

最佳答案

可能有一种非常简单的方法来执行此操作,但我不知道它是什么。刚开始使用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


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

关于nsis - NSIS-将EXE版本放入安装程序名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3039024/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com