Search code examples
c#filedirectoryfilesystemwatcher

Read files based on their modified time in C#


I have a situation here. I want to read files based on their creation of last modified time. Initially i used FileSystemWatcher so that i was notified when a new file was coming, but later i realized that if the system on which my software is running goes down or restarts the location where files were being dropped will still continue.

To make it easier for understanding i will give an example:

System A - File Server (Files are created every 2 min in a directory on this server) System B - My Software will run and Monitor files from the Path of System A

If System B goes restarts and is up again after 10 min the FileSystemWatcher will skip all these files which were generated in those 10 min.

How Can I ensure that those files generated in those 10 min of time are also captured?

Let me know if my question is still not understandable.


Solution

  • If you don't want to split it up in two systems, you have to persist a little bit of information. You could store the current timestamp in a file, every time a new event was fired on the filesystem watcher. Every time your service starts, you can read all files from the filesystem that are newer than the last timestamp. This way you shouldn't miss a file.