If you’re a programmer, there’s a good chance you’ve written some code that takes a file from a folder, opens it up and does something with the contents. This is common for ETL processes (that’s Extract, Transform and Load processes) where you’re likely manipulating data from some external source and expertly massaging it into a database. Most of us have done this at some point in our career, usually extracting data from a horrible plain text flat file to get it loaded up into a nice, relational database table.

What I’ve typically seen programmers do in this instance is create a service or program that runs on a schedule, say once an hour. It checks for changes to the target folder, and grabs any new files that haven’t been loaded yet. If you need near-real-time processing, perhaps it’s scheduled to run every minute, every second, millisecond, or maybe even a constant loop! That’s not so great for system resources. And as you’re no doubt expecting at this point, I’ve got a more elegant solution.

That solution is called the FileSystemWatcher object. Basically, this nifty thing watches a folder and raises an event the moment a file is created (or whatever notification filters you tell it to watch). So, you can get real-time results without crazy schedules or loops.

There’s a problem with being so timely, though. Depending on the size of the file or how you’re getting it, it might still be in the process of being written when the watcher object finds it. This is especially a problem if the file is coming via FTP, or somewhere across the interwebs. If it’s still being written, it can’t be opened for read/write as it’s not available for file system access yet. So, you can add a little helper function that waits until the file is fully done and ready for use before you’re notified to begin processing.

Here’s the sample code for use in a C# console application. It watches for a ZIP file to show up in a specified folder:


Like this post? Share it!