NSIS - 将 EXE 版本放入安装程序的名称中 [英] NSIS - put EXE version into name of installer

查看:109
本文介绍了NSIS - 将 EXE 版本放入安装程序的名称中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

NSIS has a Name variable that you define in the script:

Name "MyApp"

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

It defines the name of the installer, that gets displayed as the window title, etc.

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

Is there a way to pull the .NET Version number out of my main EXE and append it to the Name?

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

So that my installer name would automatically be 'MyApp V2.2.0.0" or whatever?

推荐答案

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

There might be a very simple way to do this, but I don't know what it is. When I first started using NSIS, I developed this workaround to suit my needs and haven't revisited the problem since to see if there's anything more elegant.

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

I wanted my installers to have the same version number, description, and copyright info as my main executable. So I wrote a short C# application called GetAssemblyInfoForNSIS that pulls that file info from an executable and writes it into a .nsh file that my installers include.

这是 C# 应用程序:

Here is the C# app:

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");
            }
        }
    }
}

因此,如果您像这样使用该应用程序:

So if you use that application like so:

GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh

GetAssemblyInfoForNSIS.exe MyApp.exe MyAppVersionInfo.nsh

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

You would get a file named MyAppVersionInfo.nsh that looks something like this (assuming this info is in your executable):

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

在我的 NSIS 脚本的顶部,我做这样的事情:

At the top of my NSIS script, I do something like this:

!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<中使用的文件名/a> 调用 GetAssemblyInfoForNSIS.exe.此系统调用发生在安装程序的编译期间,并在您包含它之前生成 .nsh 文件.我使用/NONFATAL 开关,这样如果在生成包含文件时发生错误,我的安装程序就不会完全失败.

The first 3 defines set up the file names to use in the !system call to GetAssemblyInfoForNSIS.exe. This system call takes place during your installer's compilation and generates the .nsh file right before you include it. I use the /NONFATAL switch so that my installer doesn't fail completely if an error occurs in generating the include file.

这篇关于NSIS - 将 EXE 版本放入安装程序的名称中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆