如何判断某个文件是打开的?(100分)

  • 主题发起人 主题发起人 xuekunli
  • 开始时间 开始时间
X

xuekunli

Unregistered / Unconfirmed
GUEST, unregistred user!
如何判断某个文件是打开的?
 
if not deleteFile(strFile) then<br>&nbsp; showmessage('该文件被打开...')
 
function IsFileInUse(fName: string): boolean;<br>var<br>&nbsp; HFileRes : HFILE;<br>begin<br>&nbsp; Result := false;<br>&nbsp; if not FileExists(fName) then exit;<br><br>&nbsp; HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);<br>&nbsp; Result := (HFileRes = INVALID_HANDLE_VALUE);<br>&nbsp; CloseHandle(HFileRes);<br>end;
 
用独占方式打开,如果失败则已经打开
 
你可以这样<br>function IsFileInUse(fName : string) : boolean;<br>var<br>&nbsp; &nbsp;HFileRes : HFILE;<br>begin<br>&nbsp; &nbsp;Result := false;<br>&nbsp; &nbsp;if isfileinuse1(extractfilename(fname)) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;if not FileExists(fName) then<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp;HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0 , nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);<br>&nbsp; &nbsp;Result := (HFileRes = INVALID_HANDLE_VALUE);<br>&nbsp; &nbsp;if not Result then<br>&nbsp; &nbsp;CloseHandle(HFileRes);<br>end;
 
后退
顶部