inno-setup - 在 Inno Setup 中解析键值文本文件以检查版本号

标签 inno-setup pascalscript

我正在为我的应用程序创建一个 Inno Setup 安装程序/更新程序。现在我需要找到一种方法来检查新版本是否可用,如果可用,则应自动安装在已安装的版本上。

特殊情况是版本号与其他数据位于一个文件中。 Inno Setup 需要读取的文件如下所示:

#Eclipse Product File
#Fri Aug 18 08:20:35 CEST 2017
version=0.21.0
name=appName
id=appId

我已经找到了一种使用脚本更新应用程序的方法,该脚本仅读取其中包含版本号的文本文件。 Inno setup: check for new updates

但就我而言,它包含安装程序不需要的更多数据。有人可以帮我构建一个可以从文件中解析版本号的脚本吗?

我已有的代码如下所示:

function GetInstallDir(const FileName, Section: string): string;
var
  S: string;
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;    
  Lines: TArrayOfString;
begin
  Result := '';
Log('start');
  if LoadStringsFromFile(FileName, Lines) then
  begin
Log('Loaded file');
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do

Log('File line ' + lines[SectionLine]);


    if (pos('version=', Lines[SectionLine]) <> 0) then
                begin
                  Log('version found');
                  S := RemoveQuotes(Trim(Lines[SectionLine]));
                  StringChangeEx(S, '\\', '\', True);
                  Result := S;
                  Exit;
                end;
    end;
end;

但是运行脚本时,检查版本字符串是否在线的检查不起作用。

最佳答案

您的代码几乎是正确的。您只缺少要在 for 循环中重复的代码周围的 beginend 。因此只有 Log 行重复;并且针对超出范围的 LineCount 索引执行 if

如果你更好地格式化代码,就会变得很明显:

function GetInstallDir(const FileName, Section: string): string;
var
  S: string;
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;    
  Lines: TArrayOfString;
begin
  Result := '';
  Log('start');
  if LoadStringsFromFile(FileName, Lines) then
  begin
    Log('Loaded file');
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do
    begin { <--- Missing }
      Log('File line ' + lines[SectionLine] );

      if (pos('version=', Lines[SectionLine]) <> 0) then
      begin
        Log('version found');
        S := RemoveQuotes(Trim(Lines[SectionLine]));
        StringChangeEx(S, '\\', '\', True);
        Result := S;
        Exit;
      end;
    end; { <--- Missing }
  end;
end;

关于inno-setup - 在 Inno Setup 中解析键值文本文件以检查版本号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790351/

相关文章:

inno-setup - 通过【代码】指定注册表卸载键位置/hive

installation - Inno Setup 中的函数指针

regex - Inno Setup 中字符串的正则表达式

mysql - 从 zip 安装 MySQL Community Server 后的密码

installation - Inno Setup卸载可执行文件的位置和名称

inno-setup - 根据 Windows 版本在 Inno Setup Run 部分执行不同的命令 block

inno-setup - 在代码部分中递归地设置Inno Setup : copy folder,子文件夹和文件

inno-setup - Inno Setup 在自定义页面上放置图像/控件

delphi - 如何将接口(interface)对象传递给 Pascal Script 函数调用?

inno-setup - 如何引用外部文件夹中的文件以便它仍然包含在安装程序包中?