【问题标题】:Call Inno Setup AfterInstall function only when the file is not skipped due to onlyifdoesntexist flag仅当由于 onlyifdoesntexist 标志而未跳过文件时才调用 Inno Setup AfterInstall 函数
【发布时间】:2020-04-01 16:07:31
【问题描述】:

AfterInstall 函数总是被调用,即使文件已经存在:

[Files]
Source: "myfile.txt"; DestDir: "{app}"; Flags: onlyifdoesntexist; \
    AfterInstall: MyAfterInstall('{app}\myfile.txt')

如果“myfile.txt”已经存在,它不会被覆盖(未安装)并且在这种情况下不应调用AfterInstall(在我看来)。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    您可以使用Check parameter 代替onlyifdoesntexist 标志。

    [Files]
    Source: "MyProg.exe"; DestDir: "{app}"; Check: OnlyIfDoesntExist; \
        AfterInstall: MyAfterInstall
    
    [Code]
    
    function OnlyIfDoesntExist: Boolean;
    var
      FileName: string;
    begin
      FileName := ExpandConstant(CurrentFilename);
      Result := not FileExists(FileName);
      if Result then
      begin
        Log(Format('Installing "%s" as the file does not exists yet.', [FileName]));
      end
        else
      begin
        Log(Format('Skipping "%s" as the files exists already.', [FileName]));
      end;
    end;
    
    procedure MyAfterInstall;
    begin
      Log(Format('Installed "%s".', [ExpandConstant(CurrentFilename)]));
    end;
    

    请注意,Check 函数被多次调用,因此您将在日志中多次看到相应的消息。如果你碰巧把检查变成了一个昂贵的函数,你最好缓存结果。

    另外请注意,您不必将文件名传递给您的AfterInstall 过程,因为您可以使用CurrentFilename function 检索名称。

    【讨论】:

    • 正是我想要的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多