Inno Setup FileExists 无法找到现有文件

Inno Setup FileExists unable to find existing file

在我的脚本中,我正在检查目录中的一个目录和两个文件是否存在。虽然第一个 returns 是正确的值,但第二个检查没有。我已经多次检查这些文件是否存在于指定目录中,但 Inno Setup 总是告诉我它们不存在。这是在虚拟 Windows 服务器上发生的,无法在我的本地计算机上重现。它总是 returns 正确的值。

UpdatePath := ExpandConstant('{app}');
if DirExists(UpdatePath) then begin
    ExePath := UpdatePath+'\Application.exe';
    ConfigFilePath := UpdatePath+'\Application.exe.config'; 
    if FileExists(ExePath) and FileExists(ConfigFilePath) then begin //this one returns incorrect values
        //Do Stuff
    end else begin
        MsgBox(FmtMessage(CustomMessage('DirPageFileNotFound'), [ExePath, ConfigFilePath]),mbInformation,MB_OK); 
        Result := False;
    end;
end else begin
    MsgBox(FmtMessage(CustomMessage('DirPageDirectoryNotFound'), [UpdatePath]),mbInformation,MB_OK);
    Result := False;
end;

如您所见,我正在检查双击也可以执行的可执行文件。它在那里,但 Inno Setup 总是告诉我它不在那里。是不是虚拟环境搞砸了?这里发生了什么?

要调试该问题,请尝试添加以下代码。然后检查安装程序的日志文件和dir命令的输出:

#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif

function GetFileAttributes(lpFileName: string): DWORD;
  external 'GetFileAttributes{#AW}@kernel32.dll stdcall'; 

function GetLastError() : LongInt;
 external 'GetLastError@kernel32.dll stdcall';

const
  INVALID_FILE_ATTRIBUTES = $FFFFFFFF;

procedure ...;
var
  UpdatePath: string;
  ExePath: string;
  FindRec: TFindRec;
  Attrs: DWORD;
  LastError: LongInt;
  ResultCode: Integer;
begin
  Log('InitializeWizard');
  UpdatePath := ExpandConstant('{app}');
  ExePath := UpdatePath+'\Application.exe';

  if FileExists(ExePath) then
  begin
    Log(ExePath + ' exists');
  end
    else
  begin
    LastError := GetLastError;
    Log(ExePath + ' does not exist - '  +
      Format('System Error. Code: %d. %s', [LastError, SysErrorMessage(LastError)]));
  end;

  if not FindFirst(UpdatePath + '\*', FindRec) then
  begin
    LastError := GetLastError;
    Log(UpdatePath + ' not found - ' +
      Format('System Error. Code: %d. %s', [LastError, SysErrorMessage(LastError)]));  
  end
    else
  begin
    repeat
      Log('Found file: ' + FindRec.Name + ' in ' + UpdatePath);
    until not FindNext(FindRec);
  end;

  Attrs := GetFileAttributes(ExePath);
  if Attrs <> INVALID_FILE_ATTRIBUTES then
  begin
    Log(ExePath + ' attributes = ' + IntToStr(Attrs));
  end
    else
  begin
    LastError := GetLastError;
    Log(Format('Cannot get attributes of ' + ExePath + ': System Error. Code: %d. %s', [
          LastError, SysErrorMessage(LastError)]));  
  end;

    Exec(ExpandConstant('{cmd}'), '/k dir "' + UpdatePath + '"', '', SW_SHOW,
      ewWaitUntilTerminated, ResultCode);
end;

FileExists internally uses FindFirst/FindNext and GetFileAttributes。所以这是为了找出问题的原因。


我的大胆猜测是目标机器是 64 位的,并且出于某种原因跳入了文件系统重定向。

在调用 FileExists:

之前尝试使用 EnableFsRedirection 禁用重定向
EnableFsRedirection(False);