想在使用AssignFile之前判断一下,避免冲突

解决方案 »

  1.   

    有个么么IsFileInUse的函数,在网上有代码的,不过我现在贴不上来,一来是VB代码,二来不在这机器上,再写一个实在麻烦着。
      

  2.   

    function IsFileInUse(fName : string ) : boolean; 
    var
      HFileRes : HFILE;
    begin
      Result := false;
      if not FileExists(fName) then
        exit;
      HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
      Result := (HFileRes = INVALID_HANDLE_VALUE);
      if not Result then
        CloseHandle(HFileRes);
    end;
    如何?
      

  3.   

    呵呵~~ 楼上的名字  :超SB  IRD
      

  4.   

    WIN32API有一函数OpenFile,其中的第三个参数有一标志:
    OF_SHARE_EXCLUSIVE Opens the file with exclusive mode, denying both read and write access to other processes. If the file has been opened in any other mode for read or write access, even by the current process, the function fails.Delphi中的FileOpen的第二个参数有对应的fmShareExclusive,含上即可。function IsFileOpened( const filename: string ): boolean;
    var
      h: integer;
    begin
      if FileExists(filename) then
      begin
        result := false;
        exit;
      end;
      h := FileOpen( filename, fmShareExclusive ); // 发生错误返回-1
      result := (h=-1);
      if not result then
        FileClose( h );
    end;