If we can't open the file, it's still copying One exception per file rather than several like in the polling pattern The file has completed arrived, so lets process it If (!IsFileReady(e.FullPath)) return //first notification the file is arriving Private void FileChanged(object sender, FileSystemEventArgs e) Watcher.Changed += new System.IO.FileSystemEventHandler(FileChanged) Watcher.NotifyFilter = System.IO.NotifyFilters.LastWrite Watcher = new System.IO.FileSystemWatcher() With that in mind I came up with some code that looks like this: class WatcherService
That second event is the one I was interested in. I also noticed that it fires a NotifyFilters.LastWrite event once when the file begins writing, and then again when the file completes writing. What I found is that the FileSystemWatcher sends multiple events over the course of a file being written to disk. I wanted to use the FileSystemWatcher if possible, but I also knew I didn’t care for the exception handling solution that my colleague ended up with.Īfter discovering that FileSystemWatcher exposes several notification options that allow you to get notified when a file has arrived or changed in some way, I came up with a solution using the FileSystemWatcher NotifyFilters.LastWrite option. Not satisfied with his solution, I went in search of a less brute force solution without reinventing the proverbial wheel. My only previous experience with System.IO.FileSystemWatcher was indirectly through my colleague. His (and my) learning experience inspired me to share it with you. Regardless of the polling interval, there were several exceptions being generated prior to a single file getting processed.Īfter a little research, my colleague came to the realization that this was a standard pattern when using System.IO.FileSystemWatcher. My first reaction was: “There must be a better way! Why would you want to incur the overhead of unwinding the stack multiple times just to find out when your file is ready for processing?” These files were taking several minutes to upload. The file is still arriving, give it time to finish copying and check again Using (var file = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)) The pseudo code looked something like this: bool fileIsBusy = true I was surprised, however, when he told me that his solution was to enter into a polling loop when the FileSystemWatcher fires an event to attempt opening the file and eat the IOException until the file had fully arrived. The result being a System.IO.IOException. In fact, due to their size the file transfer latency was causing issues with the FileSystemWatcher firing notifications before the files had completely been written to disk. I guessed it was not as straight forward as it sounded. Turns out, the files he was processing were decent sized. The design was pretty straight forward: monitor a file share for incoming files, parse each file after it arrived, and then persist the information gleaned from each file into a database for downstream processing.Īfter hearing several “choice words” emanating from his cube. NET System.IO.FileSystemWatcher to process files arriving via FTP.
A while back a colleague of mine was developing a Windows Service using the.