A
Armin Zingler
Kristoffer Persson said:I have a FileSystemWatcher in my application that triggers on file
(*.xml) creation and renaming in a folder. When I detect a new file I
should read it, but sometimes it will fail (file in use).
It is an FTP server that writes the file, so it will create the file
and write to it little by little. So after I detect the file, it can
be a while (time depending on the connection) before it is
complete.
QUESTION:
When the file is detected, I want to make sure it is complete and
free to use before I pass it on to the reading function. HOW do I
accomplish that? I have tried two ways:
1. Open it in a FileShare.None mode.
This didn't work, since it actually succeeded sometimes, causing the
FTP server some writing problems, since _I_ locked the file for
_HIM_.
2. Monitoring changes in file size.
Didn't work since the file size reported was the file size for a
completely downloaded file and not the partial download.
Any ideas?
- Kristoffer -
No way. Try to open the file in the file mode you need. If you succeed it's
ok, if not, it failed. Sounds simple, but that's how it is. I guess if file
access succeeds, the file has been written completely, but there's no
guarantee. If the FTP server is "clever", it should lock the file til it's
written completely, so succeeding in opening it should be a sign that it's
ready to be processed. But, let's assume the file is created first, then
written into it and it gets closed between the two steps even for a very
short period of time. If you happen to open it just between these two steps,
you've got a problem. So, you must hope for a well designed application
writing the file.
--
Armin
How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html