【问题标题】:Inno Setup FileExists unable to find existing fileInno Setup FileExists 无法找到现有文件
【发布时间】:2015-11-20 07:23:10
【问题描述】:

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

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 总是会告诉我它不在那里。是虚拟环境搞砸了吗?这里发生了什么?

【问题讨论】:

  • 什么检查失败了? FileExists(ExePath)? ExePath确切值是多少?
  • 该系统上的确切值是 E:\Program Files(x86)\Application\Application.exeE:\Program Files(x86)\Application\Application.exe.config。哪一个失败了?我会说两者,因为两个文件都在同一个位置。
  • 尝试在if FileExists(ExePath)之前添加if not FindFirst(UpdatePath + '\*', FindRec) then Log('Not found') else repeat Log(FindRec.Name); until not FindNext(FindRec);。它会在日志中记录什么? (代码需要FindRec: TFindRec; 变量)。
  • 我会的,但这需要一些时间,因为我必须与我们的一位客户交谈,并稍微重写此部分。
  • 那么,还要添加什么?到目前为止,我已经添加了你的循环。

标签: inno-setup pascalscript


【解决方案1】:

要调试问题,请尝试添加以下代码。然后查看安装程序的日志文件和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 内部使用FindFirst/FindNextGetFileAttributes。所以这是为了找出导致问题的原因。


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

在调用FileExists之前尝试使用EnableFsRedirection禁用重定向:

EnableFsRedirection(False);

【讨论】:

  • 天哪,我完全忘记了这个问题。查看我的代码,您对 64 位和文件系统重定向的猜测是正确的。可悲的是,我不再知道确切的结果,但我添加了一个 64 位检查,如果是这种情况,我使用了EnableFsRedirection(False)。在这种情况下,您的日志记录似乎也对我有所帮助,因为它仍然存在于我的代码中。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
相关资源
最近更新 更多