determine if lock on file

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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
}
 
P

Peter Morris

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, ...........);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top