//判断文件是否被占用函数 function IsFileInUse(FileName: TFileName): Boolean; var HFileRes: HFILE; begin Result := False; if not FileExists(FileName) then Exit; HFileRes := CreateFile(PChar(FileName), 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;
procedure TForm1.Button1Click(Sender: TObject); begin if IsFileInUse('c:\Programs\delphi6\bin\delphi32.exe') then ShowMessage('File is in use.'); else ShowMessage('File not in use.'); end;
----------------------------------------------
谢谢各位大大
var hfileres: hfile; begin Application.ProcessMessages; Result := false; if not FileExists(AName) then Exit; SetFileReadOnly(AName, false); //只读的话,下面的检测就会失败//hsj 2017-09-07 Application.ProcessMessages; hfileres := createfile(PChar(AName), generic_read or generic_write, 0, nil, open_existing, file_attribute_normal, 0); try Result := hfileres = invalid_handle_value; finally if not Result then CloseHandle(hfileres); end; end;
----------------------------------------------
qq:171833017,靖源软件http://www.dxmylove.com
// 检查文件是否被独占打开。 Function IsFileInUseForExclusiveOpen(FileName: WideString): Boolean; var hFile: THandle; begin Result := False; if not FileExists(FileName) then Exit; hFile := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite or fmShareDenyRead); if hFile = NULL_Handle then Exit; if (hFile = INVALID_HANDLE_VALUE) then begin Result := True; Exit; end; try Result := False; finally CloseHandle(hFile); end; end;
// 试图独占打开前,可以使用本函数判断 Function IsFileCanExclusiveOpen(FileName: WideString): Boolean; var hFile: THandle; begin Result := False; if not FileExists(FileName) then Exit; hFile := FileOpen(FileName, fmOpenReadWrite or fmShareExclusive); if hFile = NULL_Handle then Exit; if (hFile = INVALID_HANDLE_VALUE) then begin Result := False; //注意 和别的函数是反的。 Exit; end; try Result := True; //注意 和别的函数是反的。 finally CloseHandle(hFile); end; end;
// 试图复制到或写入文件时,可以使用本函数判断。 Function IsFileInUseForCopyToOrWrite(FileName: WideString): Boolean; var hFile: THandle; begin Result := False; if not FileExists(FileName) then Exit; hFile := FileOpen(FileName, fmOpenReadWrite or fmShareDenyNone); if hFile = NULL_Handle then Exit; if (hFile = INVALID_HANDLE_VALUE) then begin Result := True; Exit; end; try Result := False; finally CloseHandle(hFile); end; end;
// 试图删除或重命名文件时,可以使用本函数判断。 Function IsFileInUseForDeleteOrRename(FileName: WideString): Boolean; var hFile: THandle; begin Result := False; if not FileExists(FileName) then Exit; hFile := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite); if hFile = NULL_Handle then Exit; if (hFile = INVALID_HANDLE_VALUE) then begin Result := True; Exit; end; try Result := False; finally CloseHandle(hFile); end; end;
Function IsFileCanDeleteOrRename(FileName: WideString): Boolean; begin Result := not IsFileInUseForDeleteOrRename(FileName); end;
Function IsFileCanCopyToOrWrite(FileName: WideString): Boolean; begin Result := not IsFileInUseForCopyToOrWrite(FileName); end;
----------------------------------------------
(C)(P)Flying Wang