FileSystemWatcher.. easy

M

Markus Stoeger

Hello!

I'm using the FileSystemWatcher to get notified when new files are
created in a directory. Once a file appears in the directory I want to
read it's contents into memory and do some processing.

The problem I'm having is that sometimes the FileSystemWatcher event
gets fired before the file is fully written (it gets fired when the file
is created.. not when it is created+closed).

Is there some way to get notified when the new file is completely
written (closed)?

I'm currently starting another thread that I'm passing the file names
to.. it uses a while loop with a Thread.Sleep to test if the file is
complete before it continues with the processing. But that sucks.

thanks,
Max
 
M

Markus Stoeger

Octavio said:

thanks, that's describing exactly my problem. I'm not sure if I'm
correctly understanding the solution though. As far as I understand,
closing the file will fire the "Changed" event.

Please take a look at the following code. Every time the "Changed" event
gets fired, I'm trying to open the file (with FileShare.None, so I can
be sure I'm the only one who's got the file open). This should only
finish successfully when the file has been closed, shouldn't it?

private void fsw_Changed(object sender, FileSystemEventArgs e) {
System.Diagnostics.Trace.WriteLine("Changed: " + e.Name + ", " +
e.ChangeType);

FileStream fs = tryOpen(e.FullPath);

if (fs != null) {
// do my processing...
fs.Close();
}
}

// return FileStream when open was successful, otherwise null
private FileStream tryOpen(string path) {
FileStream fs = null;

try {
fs = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.None);
}
catch (Exception ex) {
if (fs != null) {
fs.Close();
fs = null;
}
}

return fs;
}

thanks,
Max
 
O

Octavio Hernandez

thanks, that's describing exactly my problem. I'm not sure if I'm
correctly understanding the solution though. As far as I understand,
closing the file will fire the "Changed" event.

I think you've correctly understood.
Please take a look at the following code. Every time the "Changed" event
gets fired, I'm trying to open the file (with FileShare.None, so I can be
sure I'm the only one who's got the file open).

Seems correctly, carefully programmed AFAICT... What does happen whe you run
this code?

Regards - Octavio
 
M

Markus Stoeger

Octavio said:
I think you've correctly understood.


Seems correctly, carefully programmed AFAICT... What does happen whe you run
this code?

I have implemented it in two of my projects that use the file system
watcher today. It seems to work fine so far. I have additionally set the
NotifyFilter to LastWrite to reduce the number of fired events.

So far it hasn't missed any files yet, but I'm still a bit skeptical
about that. Because if I write lots of data to a file and flush the
stream after every few kB it only occassionally fires the Changed
event.. so it seems like it misses a few. I guess further testing will
show how well it really works.

The only thing I don't like about it is the try/catch around the opening
of the file. Exceptions are slow and ugly and should only be used for...
_exceptions_. Not for testing for things. But I don't see a better way
right now to find out if the file has been closed already.. so I guess
it's ok.

thanks for your help,
Max
 

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