D
David Jackson
Hello,
I'm presently writing a Windows service which waits for files to be
downloaded from various completely separate remote systems into a specific
network folder (the service itself doesn't do the downloads) and then
processes them and consolidates them into the company database.
Therefore I thought that FileSystemWatcher would be a good solution for
this.
However, I soon noticed that the Created event fires as soon as a file
begins to be written, and that the FileSystemWatcher class doesn't appear to
have any method or event to say that the creation process has completed. As
some of these files can be quite large (several GB), the write process can
take several minutes. This initially caused me some problems because the
service tried to begin processing the file before it had been completely
written.
After doing some research on Google, MSDN and other places, there seemed to
be two generally accepted solutions for this problem.
1) As soon as the Created event fires, make the thread sleep for, say 30
seconds to allow the file to be completely written
2) Set up a loop to keep checking if it's possible to open the file
exclusively, e.g.
bool IsFileClosed(FileInfo fi)
{
bool isFileClosed = false;
try
{
using (FileStream fs = fi.Open( FileMode.Open, FileAccess.ReadWrite,
FileShare.None))
{
fs.Close();
return true;
}
}
catch
{
return false;
}
}
I appreciate that the second option is probably more reliable because there
is no guarantee that the number of seconds that the thread is put to sleep
will be sufficient for the file to be written, but I'd be grateful to know
if anyone has a better solution, or any comments on the above.
Thank you.
DJ
I'm presently writing a Windows service which waits for files to be
downloaded from various completely separate remote systems into a specific
network folder (the service itself doesn't do the downloads) and then
processes them and consolidates them into the company database.
Therefore I thought that FileSystemWatcher would be a good solution for
this.
However, I soon noticed that the Created event fires as soon as a file
begins to be written, and that the FileSystemWatcher class doesn't appear to
have any method or event to say that the creation process has completed. As
some of these files can be quite large (several GB), the write process can
take several minutes. This initially caused me some problems because the
service tried to begin processing the file before it had been completely
written.
After doing some research on Google, MSDN and other places, there seemed to
be two generally accepted solutions for this problem.
1) As soon as the Created event fires, make the thread sleep for, say 30
seconds to allow the file to be completely written
2) Set up a loop to keep checking if it's possible to open the file
exclusively, e.g.
bool IsFileClosed(FileInfo fi)
{
bool isFileClosed = false;
try
{
using (FileStream fs = fi.Open( FileMode.Open, FileAccess.ReadWrite,
FileShare.None))
{
fs.Close();
return true;
}
}
catch
{
return false;
}
}
I appreciate that the second option is probably more reliable because there
is no guarantee that the number of seconds that the thread is put to sleep
will be sufficient for the file to be written, but I'd be grateful to know
if anyone has a better solution, or any comments on the above.
Thank you.
DJ