如何检测文件是否将被安装(或替换)?

时间:2014-03-28 10:20:03

标签: inno-setup

我有一个Inno Setup脚本,我需要在安装或替换特定文件之前执行自定义操作。

documentation州:

  

如果安装程序已确定不应处理的条目,则不会调用BeforeInstallAfterInstall函数。

所以我认为我可以使用BeforeInstall来指定在安装文件之前应该做什么,但似乎Inno Setup决定安装文件还是调用之后BeforeInstall函数,所以每次都会调用此函数。

如何在安装文件之前执行操作,但是如果不安装文件(例如因为其版本没有更改)则不能执行操作?

编辑:

以下是代码的相关部分:

...
[Files]
...
Source: "..\bin\{#BuildConfig}\FooBar.dll"; DestDir: "{app}"; Flags: uninsrestartdelete; BeforeInstall: PrepareInstallFooBar

...
[Code]
...
procedure PrepareInstallFooBar();
begin
  Log(Format('BeforeInstall: %s', [CurrentFileName]));
  ...
end;

...

以下是日志显示的内容:

...
2014-03-28 10:40:46.778   BeforeInstall: {app}\FooBar.dll
2014-03-28 10:40:46.778   -- File entry --
2014-03-28 10:40:46.778   Dest filename: C:\Users\tom\AppData\Local\MyApp\bin\FooBar.dll
2014-03-28 10:40:46.778   Time stamp of our file: 2014-03-28 10:35:10.000
2014-03-28 10:40:46.778   Dest file exists.
2014-03-28 10:40:46.778   Time stamp of existing file: 2014-03-28 10:35:10.000
2014-03-28 10:40:46.778   Version of our file: 1.0.0.0
2014-03-28 10:40:46.778   Version of existing file: 1.0.0.0
2014-03-28 10:40:46.778   Same version. Skipping.
...

1 个答案:

答案 0 :(得分:0)

由于我无法使Inno Setup按照我想要的方式运行,我最终在Check函数中检查了该版本:

...
[Files]
Source: "..\bin\{#BuildConfig}\FooBar.dll"; DestDir: "{app}"; Flags: uninsrestartdelete; Check: ShouldInstallFooBar; BeforeInstall: BeforeInstallFooBar;
...

[Code]
function ShouldInstallFooBar() : Boolean;
var
  fooBarTempPath: String;
  installedFooBarPath: String;
  newFooBarVersion: String;
  installedFooBarVersion: String;
begin
  installedFooBarPath := ExpandConstant('{app}\FooBar.dll');
  if FileExists(installedFooBarPath) then begin
    // Get version of currently installed file
    if GetVersionNumbersString(installedFooBarPath, installedFooBarVersion) then begin
      Log('File already exists; version = ' + installedFooBarVersion);
      // Extract new file and get its version
      ExtractTemporaryFile('FooBar.dll');
      fooBarTempPath := ExpandConstant('{tmp}\FooBar.dll');
      if GetVersionNumbersString(fooBarTempPath, newFooBarVersion) then begin
        Log('New version = ' + newFooBarVersion);
        if newFooBarVersion <> installedFooBarVersion then begin
          fooBarShouldBeInstalled := true;
        end else begin
          fooBarShouldBeInstalled := false;
        end;
      end else begin
        // Failed to get version for new file; assume it's different and install it
        fooBarShouldBeInstalled := true;
      end;
    end else begin
      // Failed to get version for existing file; assume it's different and install the new one
      fooBarShouldBeInstalled := true;
    end;
  end else begin
    // File doesn't exist, install it
    fooBarShouldBeInstalled := true;
  end;
  Result := fooBarShouldBeInstalled;
end;

procedure BeforeInstallFooBar();
begin
  // what I need to do before the file is installed...
end;

...