Inno setup Inno安装加载字符串,获取数字并添加

Inno setup Inno安装加载字符串,获取数字并添加,inno-setup,Inno Setup,我需要inno设置来连线,检查区域的数量。并添加+1,请参见下面的示例 Original FILE: [Area.1] Title=World P1 Local=C:\scenery\world\p Layer= Active=TRUE Required=FALSE [Area.2] Title=World C1 Local=C:\scenery\world\c Layer= Active=TRUE Required=FALSE [Area.3] Title=World D1 Local=

我需要inno设置来连线,检查区域的数量。并添加+1,请参见下面的示例

Original FILE:

[Area.1]
Title=World P1
Local=C:\scenery\world\p
Layer=
Active=TRUE
Required=FALSE

[Area.2]
Title=World C1
Local=C:\scenery\world\c
Layer=
Active=TRUE
Required=FALSE

[Area.3]
Title=World D1
Local=C:\scenery\world\d
Layer=
Active=TRUE
Required=FALSE

[Area.4]
Title=World E1
Local=C:\scenery\world\e
Layer=
Active=TRUE
Required=FALSE
Inno安装程序将检查案例区域中的最后一个区域。4,它将获取数字并将其添加为+1,然后再添加一个带有附加数字的区域,以便能够按照所述跟踪文件。 所以,取面积4,再加上

[第5区]

标题=世界F1

Local=C:\scapeuting\world\f

层=

活动=真

必需=错误

Inno安装,阅读并检查最后一个区域,安装后,它将保持这样

[Area.1]
Title=World P1
Local=C:\scenery\world\p
Layer=
Active=TRUE
Required=FALSE

[Area.2]
Title=World C1
Local=C:\scenery\world\c
Layer=
Active=TRUE
Required=FALSE

[Area.3]
Title=World D1
Local=C:\scenery\world\d
Layer=
Active=TRUE
Required=FALSE

[Area.4]
Title=World E1
Local=C:\scenery\world\e
Layer=
Active=TRUE
Required=FALSE

[Area.5]
Title=World F1
Local=C:\scenery\world\f
Layer=
Active=TRUE
Required=FALSE
我正在使用这段代码,但它只是添加,我需要安装程序检查原始文件中的数字,并更改行[1]添加+1,就像它是PHP/mysql中的总和一样

function saveStringToFile(): boolean;
var
  InstallDir: string;
  fileName : string;
  lines : TArrayOfString;
begin
  if FileExists(ExpandConstant('{app}\scenery.cfg')) then
  begin
    MsgBox('Archive "scenery.cfg" found', mbInformation, MB_OK);
    Result := True;
    fileName := ExpandConstant('{app}\scenery.cfg');
    SetArrayLength(lines, 43);
  //
  lines[0] := '';
  lines[1] := '[Area.5]';
  lines[2] := 'Title=World F1';
  lines[3] := 'Local=C:\scenery\world\f';
  lines[4] := 'Layer=';
  lines[5] := 'Active=TRUE';
  lines[6] := 'Required=FALSE';
  lines[7] := '';
  //
  Result := SaveStringsToFile(filename,lines,true);
  exit;
  end
  else
  begin
    MsgBox('Archive "scenery.cfg" not found', mbInformation, MB_OK);
    Result := False;
  end;
end;

您首先必须读取配置文件才能获得最大区域数。请参见以下功能。有一个单独的函数来计算某一行的编号。在达到最大数量后,只需添加1,然后继续编写您的传统区域

获得
文件名后,您可以调用:
maxNumber:=GetMaxNumber(文件名)

该函数有一些解释性注释。您还可以取消注释一些消息框,这些消息框为您提供一些正在发生的事情的信息

function GetAreaNumber(line : String) : Integer;
var
    pos1, pos2 : Integer;
    number : String;
begin
    // This function only gets called, when the line contains an area header.
    // Get the positions of the chracter before and after the number and
    // extract the number.
    pos1 := Pos('.', line)
    pos2 := Pos(']', line)
    number := Copy(line, pos1 + 1, pos2 - (pos1 + 1))
    //MsgBox(number, mbInformation, MB_OK)
    Result := StrToIntDef(number, 0)
end;

function GetMaxNumber(fileName : String): Integer;
var
    lines : TArrayOfString;
    linesCount, index : Integer;
    maxNumber, currentNumber : Integer;
begin
    maxNumber := 0

    if LoadStringsFromFile(fileName, lines) then
    begin
        linesCount := GetArrayLength(lines)
        //MsgBox(IntToStr(linesCount), mbInformation, MB_OK)

        // Run through all the lines from the file.
        for index := 0 to linesCount - 1 do
        begin
            // Check if the line contains an area header.
            if Pos('[Area.', lines[index]) > 0 then
            begin
                //MsgBox(lines[index], mbInformation, MB_OK)
                currentNumber := GetAreaNumber(lines[index])

                if currentNumber > maxNumber then
                begin
                    maxNumber := currentNumber
                end
            end
        end
    end;

    Result := maxNumber
end;

“将数字加到+1”是什么意思?是否要将
[Area.1]
更改为
Area.2
Area.2
更改为
Area.3
?你能给我们看一下原始文件吗?你想让它在安装后如何处理?@MartinPrikryl我想他想更新INI并用下一个数字添加一个新的区域部分。我想…你提到
[Area.X]
:这是否意味着,你有一个包含一些区域的大文件,你必须搜索Title=X的区域,获取其编号,然后向其中添加1?我有一个.cfg文件,其中包含[Area.1]及其行,我需要Inno设置来检查区域编号并添加6个区域,将有区域。1,2,3,4,5,6。。。就像@M.Bauer saidI编辑了这篇文章,让它更容易理解。使用
strotintdef
优雅地处理意外部分。否则,当安装程序遇到诸如
[Area.]
[Area.X]
等行时,安装程序将致命失败。您应该测试该行是否以
[Area.
开头,而不是仅包含该行。该行可以像
;[Area.1]那样注释掉
。值得考虑在循环中使用
IniKeyExists
以增量方式测试节的存在性。这将是最可靠的方法。我将字符串转换函数更改为
StrToIntDef()
正如Martin所建议的那样。@Martin:使用
IniKeyExists
需要知道起始的最小区域号。如果
[area.2]
是文件中的第一个,搜索
[area.1]
将失败,并给出错误的结果。当然,这就是我的意思“在循环中以增量方式测试节是否存在”