determine if lock on file

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

What is the best way to determine if another process/thread has a lock on a
file ?

Currently I am attempting to open file for writing and catching exception
.... hardly elegant.
 
What is the best way to determine if another process/thread has a lock ona
file ?

Currently I am attempting to open file for writing and catching exception
... hardly elegant.

It's the only way I'm afraid.
 
It's the only way I'm afraid.

Not only that, but you have to keep the file open, you can't do
something like:

if ( !IsFileLocker( filename))
{
//work with file
}
 
It's really the only reliable way. For example if you were to use some
WinAPI to check the file for locks you could experience this


if (!FileIsLocked(fileName))
{
//Too late, another thread or process has got in there first and locked
it
var fs = new FileStream(fileName, ...........);
}
 
Back
Top