如何根据我的应用程序版本自动设置Inno Setup安装程序的版本?

73

我正在使用Inno Setup生成我的应用程序的安装程序。如何将由Inno生成的setup.exe(VersionInfoVersion)的版本号自动设置为与我的应用程序的版本号匹配?现在每次部署新版本的应用程序时,我都需要手动更新版本号。

现在我是这样做的:

[Setup]
VersionInfoVersion=1.2.2.0 //writing the value manually
我想要像这样的东西:
[Setup]
VersionInfoVersion={Get the version of my app}
7个回答

109

您可以像这样使用Inno Setup预处理器GetVersionNumbersString函数:

#define ApplicationName 'Application Name'
#define ApplicationVersion GetVersionNumbersString('Application.exe')
[Setup]
AppName={#ApplicationName}
AppVerName={#ApplicationName} {#ApplicationVersion}
VersionInfoVersion={#ApplicationVersion}

7
看起来我需要输入完整的exe路径,而不仅仅是像示例中那样输入exe名称。 - NickG
4
为什么不能使用#define ApplicationVersion GetFileVersion({#ApplicationName}) - SAAD
2
@NickG:相对路径也足够了。 - Jacek Krawczyk
5
可以的,这条代码应该有效:#define ApplicationVersion GetFileVersion(ApplicationName) - Jacek Krawczyk
10
如果您已经单独指定了相关的文件夹,正确的语法应该是:#define MyAppVersion GetFileVersion(MyAppSourceFolder + '\' + MyAppExeName)。需要记住的是,{#var} 语法在此处似乎无效。 - wpfwannabe

17

使用命令行参数是另一种方法:

[Setup]           
AppVersion={#MyAppVersion}

你只需要从命令行调用你的脚本,如下所示:

cd C:\Program Files (x86)\Inno Setup 5

iscc /dMyAppVersion="10.0.0.1" "C:\MyPath\MyScript.iss"

在 iss 脚本中,它模拟了#define MyAppVersion="10.0.0.1"


如果您正在使用 CakeBuild,您可以将此参数传递。

 string CurrentVersion  = "10.0.0.1";
 InnoSetupSettings settings = new InnoSetupSettings();
 settings.Defines=   new Dictionary<string, string>
            {
            { "MyAppVersion", CurrentVersion },
            };
   InnoSetup("C:\MyPath\MyScript.iss", settings);

6
如果您有一个纯web安装程序,那么接受的解决方案将不起作用,因为您根本没有一个application.exe可以从中获取版本号。
我正在使用Nant和带有版本号属性的build.xml文件,我在重新构建innosetup安装程序之前手动提升版本号。
我的*.iss文件包含一个特殊的标记@APPVERSION@,在构建过程中将其替换为版本号。这是通过应用filterchain的复制操作完成的,如下所示。
InnoSetup脚本(*.iss)
// the -APPVERSION- token is replaced during the nant build process
#define AppVersion "@APPVERSION@"

nant build.xml:

<!-- Version -->
<property name="product.Name"           value="My Software"/>
<property name="version.Major"          value="1"/>
<property name="version.Minor"          value="2"/>
<property name="version.BuildNumber"    value="3"/>
<property name="product.Version" 
          value="${version.Major}.${version.Minor}.${version.BuildNumber}"/>

<!-- build task -->
<target name="bump-version"
        description="Inserts the current version number into the InnoScript.">
        <copy todir="${dir.Build}" overwrite="true">
            <fileset basedir="${dir.Base}/innosetup/">
                <include name="product-webinstaller-w32.iss"/>
                <include name="product-webinstaller-w64.iss"/>
            </fileset>
            <filterchain>
                <replacetokens>
                    <token key="APPVERSION" value="${product.Version}"/>
                </replacetokens>
            </filterchain>
        </copy>
</target>

4
正如其他人提到的,可以使用GetFileVersionGetStringFileInfo预处理器函数来实现这一点。
一些重要的信息、改进和有用的补充如下:
  • 您可以使用相对于 .iss 文件的路径或绝对路径指向 exe 文件。
  • 您可以通过只写名称来包含现有定义,并使用 + 运算符连接定义:
    #define MyAppPath "..\Win32\Release\" + MyAppExeName
  • 如果您希望从右侧轻松删除版本号的某些部分,可以使用 RemoveFileExt 函数,例如将 3.1.2.0 转换为 3.1.2:
    #define MyAppVersion RemoveFileExt(GetFileVersion(MyAppPath))
  • 您可以在随后的选项(如 MessagesFilesIcons)中使用 MyAppExeNameMyAppPath 定义。
工作示例:
#define MyAppName "Application Name"
#define MyAppExeName "Application.exe"        
#define MyAppPath "..\Win32\Release\" + MyAppExeName
#define MyAppVersion RemoveFileExt(GetFileVersion(MyAppPath))

[Setup]
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
VersionInfoVersion={#MyAppVersion}
OutputBaseFilename={#MyAppName}-{#MyAppVersion}-Windows

...

[Messages]
SetupWindowTitle=Setup - {#MyAppName} {#MyAppVersion}

...

[Files]
Source: {#MyAppPath}; DestDir: "{app}"; Flags: ignoreversion; Tasks: desktopicon

...

[Icons]
Name: "{group}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"

4

我遇到了一些问题,所以在这里分享我的解决方案。

app.iss:

[Setup]
#include "Config.txt"

#define AppVersion GetFileVersion("Input\" + AppExec)


AppName={#AppName}
AppVersion={#AppVersion}

Config.txt:

#define AppName "App"
#define AppExec "App.exe"

2
在我的情况下,我希望从文件中定义版本字符串。由于我的安装程序正在打包一个嵌入式Python程序,因此我没有EXE可用。所以我会像这样在一个单行文本文件中定义版本号(这是从之前的git tag语句创建的):

..\Build\app_version.txt:
v1.2.1

在Inno Setup中,我使用了预处理器定义语句来设置整个文本中的版本。
#define VerFileNum FileOpen("..\Build\app_version.txt")
#define MyAppVersion Trim(StringChange(FileRead(VerFileNum),"v",""))

在这里,我使用了Trim()StringChange()来从字符串中删除前导的“v”和尾随空格。稍后在设置部分中,可以使用预处理器定义来设置AppVersion值:

[Setup]
AppVersion={#MyAppVersion}

Inno Setup 预处理器已经定义了相当丰富的函数集:Inno Setup 预处理器函数。请注意保留 HTML 标签,本文不会提供解释。

1

在尝试其他方法一段时间后,对我有效的方法是使用相对路径(我的.iss文件在一个文件夹中,而EXE文件在上面两个级别)。

; Extract File Version from EXE
#define MyAppVersion GetFileVersion("..\..\Release\CSClave.exe")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接