Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tell wether or not a file is in use? 1

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
As above, how do you tell wether or not a file is in use? I can of course just use Sleep(5000); which is surely long enough but I don't find this a very charming solution.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
found this on the web

In some situations of your applications you must check: the file is locked for exclusive access or not. The next function you can use for this check:
Code:
function FileInUse(FileName: string): 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;


Aaron Taylor
John Mutch Electronics
 
That's great man! Thanks!

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
E-mail me at caswegkamp@hotmail.com
Great Delphi Websites faq102-5352
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top