detect if a file is open

D

deerchao

I'm using FileSystemWatcher to watch the new file creation. When there
is one, I want to open it and read it's content. But, by the time when
I got the message from FileSystemWatcher the file may be still hold by
the process who just created it, writing data.
So is there any way I can detect if a file is open or not? Or can I
wait untill the file is free? Or there is another solution?

Thanks!
 
W

Willy Denoyette [MVP]

deerchao said:
I'm using FileSystemWatcher to watch the new file creation. When there
is one, I want to open it and read it's content. But, by the time when
I got the message from FileSystemWatcher the file may be still hold by
the process who just created it, writing data.
So is there any way I can detect if a file is open or not? Or can I
wait untill the file is free? Or there is another solution?

Thanks!


The best way to handle this is by repeatedly (waiting a few 100msecs.
between each try) trying to open the file, until it succeeds or until a
number of tries have been reached. If you own both programs, you can use a
synchronization primitive (Semaphore or Mutex) in order to control access to
the file.

Willy.
 

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