Is a file locked by another process ? How can I check ?

G

Guest

Hi I am using the filesystem watcher to pick up files as they are dropped
into a folder - the files are output file from another system and I have no
control over that.

When 'watcher raises the event that a new file has appeared then sometimes I
can read the file even tho it is still being written but it is incomplete.
Other time a file access error occurs.

I need to write a test that will see if any other process is still using
the file or if it is complete. Im using a vb.net 2005 and Net v2 A windows
service hosts the filesystem watcher which intances a class to process the
files that appear.
 
M

Michael Nemtsev

Hello Guy_B,

U need to iterate through the processes to find the file handler that belongs
to one of the processes (u need pinvoke calls)
Googling in dotnet groups to find sample.

G> Hi I am using the filesystem watcher to pick up files as they are
G> dropped into a folder - the files are output file from another system
G> and I have no control over that.
G>
G> When 'watcher raises the event that a new file has appeared then
G> sometimes I can read the file even tho it is still being written but
G> it is incomplete. Other time a file access error occurs.
G>
G> I need to write a test that will see if any other process is still
G> using the file or if it is complete. Im using a vb.net 2005 and Net
G> v2 A windows service hosts the filesystem watcher which intances a
G> class to process the files that appear.
G>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
J

John Timney \( MVP \)

An easy way would probably be to try to open it exclusively, if its being
written to you'll get an exception you can trap and re-check.

I think the syntax is:
FileStream myFile = new FileStream(name, FileMode.Open, FileAccess.Write,
FileShare.None);
 

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