system.io.filesystemwatcher

  • Thread starter Thread starter mike
  • Start date Start date
M

mike

Hi,

is anyone here that used filesystemwatcher?

I have the problem that i dont know when a file ist written successfully.

fw.NotifyFilter =

NotifyFilters.FileName |

NotifyFilters.LastWrite |

NotifyFilters.Size |

NotifyFilters.DirectoryName;



Can anybody help me?





Thanks in advance and best regards,



Mike
 
Hi Mike,
is anyone here that used filesystemwatcher?

Yep, I use it in several projects.
I have the problem that i dont know when a file ist written successfully.

Well, that depend of what you mean with successful.
The FileSystemWatcher sometimes does not works as one would expect, for
example there is not way to knowing when another process finish to write to
a file, you only can get a LastWrite notification.

Therefore sometimes if you open the file in question you will get an
exception stating that the file is in use by another process.
What to do depends of your situation. I will describe two options that I
currently use for differents scenario, please remember that these works for
my problem, you may have to change them a little.

1- Create another file to indicate when the other process finished writting
to the file,
I have a process writting a file and another process waiting to consume
it. what I do is that the producer create a done.flag file and this is the
indication for the consumer process that the file is ready to be read.

2- Intent until you can open it.
when you get the notification do a Thread.Sleep for a reasonable span.
the try to read the file, if you cannot do another sleep, something like
this:

// NOT TESTED CODE
while ( true )
{
try{
File f = File.Open ( .... );
return f;
}catch
{
Thread.Sleep( 1000 ); // 1second.
}

}



Hope this help.
 
Back
Top