使用Inno Setup中的Pascal Script在配置文件中查找和读取特定字符串

前端之家收集整理的这篇文章主要介绍了使用Inno Setup中的Pascal Script在配置文件中查找和读取特定字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有很长的配置文件,我需要从文件提取特定的字符串.
我想要提取/读取的是针对特定号码位置的InstallDir,例如为20540.

我知道如何在INI或XML中查找字符串,但无法处理这种形式的文件.

显示结构的文件片段:

"212280"
{
    "InstallDir"        "D:\\XYZ\\stu\\opr"
    "UpdateKBtoDL"      "0"
    "HasAllLocalContent"        "1"
    "UpToDate"      "1"
    "DisableAutoUpdate"     "0"
}
"20540"
{
    "UpdateKBtoDL"      "0"
    "InstallDir"        "C:\\ABC\\def\\ghi"
    "HasAllLocalContent"        "1"
    "UpToDate"      "1"
    "maintenance_time"      "1339663134"
    "DisableAutoUpdate"     "0"
}
"4560"
{
    "UpdateKBtoDL"      "0"
    "HasAllLocalContent"        "0"
    "UpToDate"      "0"
    "InstallDir"        ""
}

解决方法

你需要编写自己的解析器.这可能是一种可能的实现:
[Code]
function GetInstallDir(const FileName,Section: string): string;
var
  S: string;
  DirLine: Integer;
  LineCount: Integer;
  SectionLine: Integer;    
  Lines: TArrayOfString;
begin
  Result := '';
  S := '"' + Section + '"'; // AddQuotes is broken somehow...
  if LoadStringsFromFile(FileName,Lines) then
  begin
    LineCount := GetArrayLength(Lines);
    for SectionLine := 0 to LineCount - 1 do
      if Trim(Lines[SectionLine]) = S then
      begin
        if (SectionLine < LineCount) and (Trim(Lines[SectionLine + 1]) = '{') then
          for DirLine := SectionLine to LineCount - 1 do
          begin
            if (Pos('"InstallDir"',Lines[DirLine]) > 0) and
              (StringChangeEx(Lines[DirLine],'"InstallDir"','',True) > 0) then
            begin
              S := RemoveQuotes(Trim(Lines[DirLine]));
              StringChangeEx(S,'\\','\',True);
              Result := S;
              Exit;
            end;
            if Trim(Lines[DirLine]) = '}' then
              Exit;
          end;
        Exit;
      end;
  end;
end;

procedure InitializeWizard;
begin                         
  MsgBox(GetInstallDir('d:\File.almostjson','20540'),mbInformation,MB_OK);
end;
原文链接:https://www.f2er.com/delphi/102478.html

猜你在找的Delphi相关文章