<p>We have several .NET applications that monitor a directory for new files, using FileSystemWatcher. The files are copied from another location, uploaded via FTP, etc. When they come in, the files are processed in one way or another. However, one problem that I have never seen a satisfactory answer for is: for large files, how does one know when the files being monitored are still being written to? Obviously, we need to wait until the files are complete and closed before we begin processing them. The event args in the FileSystemWatcher events do not seem to address this.</p>

[{'answer_id': 30090, 'author': 'Chris Marasti-Georg', 'author_id': 96, 'author_profile': 'https://Stackoverflow.com/users/96', 'pm_score': 2, 'selected': True, 'text': "<p>Have you tried getting a write lock on the file?  If it's being written to, that should fail, and you know to leave it alone for a bit...</p>\n"}, {'answer_id': 30092, 'author': 'kokos', 'author_id': 1065, 'author_profile': 'https://Stackoverflow.com/users/1065', 'pm_score': 2, 'selected': False, 'text': '<p>You probably have to go with some out of band signaling: have the producer of "file.ext" write a dummy "file.ext.end".</p>\n'}, {'answer_id': 30103, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 2, 'selected': False, 'text': '<p>The "Changed" event on the FileSystemWatcher should shouldn\'t fire until the file is closed.  See my <a href="https://stackoverflow.com/questions/24315/is-it-possible-to-raise-an-event-when-a-file-becomes-accessible">answer to a similar question</a>.  There is a possibility that the FTP download mechanism closes the file multiple times during download as new data comes in, but I would think that is a little unlikely.</p>\n'}, {'answer_id': 30106, 'author': 'Ryan Ahearn', 'author_id': 75, 'author_profile': 'https://Stackoverflow.com/users/75', 'pm_score': 3, 'selected': False, 'text': "<p>If you are in control on the program that is writing the files into the directory, you can have the program write the files to a temporary directory and then move them into the watched directory.  The move should be an atomic operation, so the watcher shouldn't see the file until it is fully in the directory.</p>\n\n<p>If you are not in control of what is writing to the watched directory, you can set a time in the watcher where the file is considered complete when it has remained the same size for the given time.  If immediate processing isn't a concern, setting this timer to something relatively large is a fairly safe way to know that either the file is complete or it never will be.</p>\n"}, {'answer_id': 30108, 'author': 'Matt Lacey', 'author_id': 1755, 'author_profile': 'https://Stackoverflow.com/users/1755', 'pm_score': 2, 'selected': False, 'text': "<p>Unless the contents of a file can be verified for completion (it has a verifiable format or includes a checksum of the contents) only the sender can verify that a whole file has arrived.</p>\n\n<p>I have used a locking method for sending large files via FTP in the past.</p>\n\n<p>File is sent with an alternative extension and is renamed once the sender is happy it is all there. </p>\n\n<p>The above is obviously combined with a process which periodically tidies up old files with the temporary extension.</p>\n\n<p>An alternative is to create a zero length file with the same name but with an additonal .lck extension.  Once the real file is fully uploaded the lck file is deleted. The receiving process obviously ignores files which have the name of a lock file.</p>\n\n<p>Without a system like this the receiver can never be sure that the whole file has arrived.</p>\n\n<p>Checking for files that haven't been changed in x minutes is prone to all sorts of problems.</p>\n"}, {'answer_id': 30136, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 1, 'selected': False, 'text': "<p>+1 for using a file.ext.end signaler if possible, where the contents of file.ext.end is a checksum for the larger file.  This isn't for security so much — if someone can insert their own file into the large stream they can replace the checksum as well. But it does help make sure nothing was garbled along the way.</p>\n"}, {'answer_id': 30182, 'author': 'Matt Lacey', 'author_id': 1755, 'author_profile': 'https://Stackoverflow.com/users/1755', 'pm_score': 0, 'selected': False, 'text': "<p>A write lock doesn't help if the file upload failed part way through and the sender hasn't tried resending (and relocking) the file yet.</p>\n"}, {'answer_id': 30196, 'author': 'Kaniu', 'author_id': 3236, 'author_profile': 'https://Stackoverflow.com/users/3236', 'pm_score': 1, 'selected': False, 'text': "<p>The way I check in Windows if a file has been completely uploaded by ftp is to try to rename it. If renaming fails, the file isn't complete. Not very elegant, I admit, but it works.</p>\n"}, {'answer_id': 1431709, 'author': 'Dirk Vollmar', 'author_id': 40347, 'author_profile': 'https://Stackoverflow.com/users/40347', 'pm_score': 2, 'selected': False, 'text': '<p>The following method tries to open a file with write permissions. It will block execution until a file is completely written to disk:</p>\n\n<pre><code>/// &lt;summary&gt;\n/// Waits until a file can be opened with write permission\n/// &lt;/summary&gt;\npublic static void WaitReady(string fileName)\n{\n    while (true)\n    {\n        try\n        {\n            using (System.IO.Stream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))\n            {\n                if (stream != null)\n                {\n                    System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName));\n                    break;\n                }\n            }\n        }\n        catch (FileNotFoundException ex)\n        {\n            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));\n        }\n        catch (IOException ex)\n        {\n            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));\n        }\n        catch (UnauthorizedAccessException ex)\n        {\n            System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));\n        }\n        Thread.Sleep(500);\n    }\n}\n</code></pre>\n\n<p>(from my answer to a <a href="https://stackoverflow.com/questions/699538/file-access-error-with-filesystemwatcher-when-multiple-files-are-added-to-a-direc/699587#699587">related question</a>)</p>\n'}]
['https://Stackoverflow.com/questions/30074', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2144/']