DeleteFile无法正常工作

IEl*_*ite 3 delphi delphi-7

似乎我的代码中的某个地方我正在使用的文件以某种方式保持它,因为我似乎无法删除它.我的CopyFile例程似乎工作,但在我完成复制后,我似乎无法删除它.

   FSize:=  GetFileSizeExt(InPath + InFileName);
   if FSize <= 0 then
   begin
    //archive file
    if  AfterAction = 'MOVE' then
    begin
     tmpExt:= ExtractFileExt(InFileName);
     if CopyFile(PChar(InPath + InFileName), PChar(MovePath + '\' + ChangeFileExt(InFileName,'') + '_' + FormatDateTime('mmddyyyy-hhmmss', Now) + tmpExt), True) then
     begin
      if not DeleteFile(pchar(InPath + InFileName)) then
      begin
       ExitCode:= 8;
       raise ECustomException.Create('Invalid After Action. Error Deleting File!');
      end;
     end //if CopyFile
     else //if not DeleteFile
     begin
      ExitCode:= 16;
      raise ECustomException.Create('File Copy Error!');
     end; //else
    end; //if  AfterAction = 'MOVE' then
     ExitCode:= 17;
     raise ECustomException.Create('Error Getting file size OR file size less than or equal to zero!');
   end; //if filesize =0
Run Code Online (Sandbox Code Playgroud)

当我在线上设置断点时,
如果不是DeleteFile,
它总是会引发异常.与
CopyFile例程 中使用的InPath和InFileName匹配

无论如何,我总是得到错误尝试删除文件.这与文件大小有关吗?我只复制和删除文件大小<= 0

Cos*_*und 11

如果您不知道删除文件失败的原因,为什么不让操作系统告诉您?替换此代码:

raise ECustomException.Create('Invalid After Action. Error Deleting File!');
Run Code Online (Sandbox Code Playgroud)

RaiseLastOSError

我不认为问题与您要显示的代码有关,所以这里有一个要检查的事项列表:

  • 该文件不是只读的.
  • 该文件不在只读媒体上.
  • 运行应用程序的用户有权删除该文件.
  • 该文件未使用.

我要做的另一件事:当引发关于无法删除文件的异常时,在我在IDE中运行RUN之前,我将转到Windows资源管理器并尝试自己删除该文件.

  • 我的错!我确实以错误的顺序排列了我的代码,并且文件在上面发布的代码生效时打开了. (2认同)