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; 如何?
呵呵~~ 楼上的名字 :超SB IRD
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;
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;
如何?
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;