InnoSetup:如何通过读取文件来声明变量

rog*_*ack 12 inno-setup

好的我知道你可以在InnoSetup做到这一点:

#define AppVer "0.0.11"
Run Code Online (Sandbox Code Playgroud)

然后就像使用它一样

[Setup]
AppVerName={#AppVer}
Run Code Online (Sandbox Code Playgroud)

现在假设我有一个名为"VERSION"的文件,其内容为"0.0.11"

文件VERSION的内容是否有某种方式进入InnoSetup变量?

Mir*_*ral 22

使用ISPP的GetFileVersion功能是首选方法(因为您的安装程序版本应该与您的应用程序的版本匹配).所以如果这是你真正想做的事情,你应该接受jachguate的回答.

如果您确实想要从文本文件而不是可执行文件中读取版本,那么有两种可能性:

第一种:如果你可以修改文件的内部格式,那么你可以通过使它看起来像一个INI文件来大大简化:

[Version]
Ver=0.0.11
Run Code Online (Sandbox Code Playgroud)

鉴于此,您可以使用ISPP的ReadIni功能来检索版本:

#define AppVer ReadIni("ver.ini", "Version", "Ver", "unknown")
Run Code Online (Sandbox Code Playgroud)

第二种选择,如果你不能改变文件格式,是使用FileOpen,FileReadFileCloseISPP功能,例如:

#define VerFile FileOpen("ver.txt")
#define AppVer FileRead(VerFile)
#expr FileClose(VerFile)
#undef VerFile
Run Code Online (Sandbox Code Playgroud)

我再说一遍:最好从可执行文件本身获取应用程序版本.一方面,这有助于确保一切都匹配.

  • 更简单的可能是将[[Setup] AppVerName = 0.0.11`这样的文件和## include`这样的文件放到最终脚本中. (2认同)
  • 另请注意,对于“ReadIni”,相对路径不起作用,请参阅[Inno Setup 预处理器可以使用 FileOpen 读取文件,但不能使用 ReadIni](/sf/ask/4076216231/)。 (2认同)

jac*_*ate 8

您可以使用Inno Setup Pre Proccessor(ISPP)GetFileVersion函数直接从您的可执行文件中获取版本,如下所示:

[Setup]
#define AppVersion GetFileVersion("MyFile.exe")
#define AppName "My App name"

AppName={#AppName}
AppVerName={#AppName} {#AppVersion}
VersionInfoVersion={#AppVersion}

; etc
Run Code Online (Sandbox Code Playgroud)

如果您使用的是定义,那么您已经在使用ISPP.


Ken*_*ite 7

如果您想在安装程序的运行时执行此操作(@jachguate的答案涵盖了另一种可能性,并且可能是您正在寻找的那个 - 我会留下它,以防它在某些时候帮助其他人),您可以使用它的Pascal脚本来做到这一点.这样的事情应该有效:

[Setup]
AppVer={code:MyVersion}

[Files]
// Other files
Source: "YourVersionFile.txt"; DestDir: "{app}"

[Code]
function MyVersion(Param: String): String;
var
  VersionValue: string;
  VersionFile: string;
begin
  // Default to some predefined value.
  Result := '0.0.0';
  VersionFile := '{app}\YourVersionFile.txt';
  if LoadStringFromFile(VersionFile, VersionValue) then
    Result := VersionValue;
end;
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅帮助文件主题"Pascal Scripting",包括支持的内置函数列表.