How to monitor changes in file content?


I need to write a simple(?) component that will monitor the changes being made to a text file and notify them to registered listeners.

I have a process which writes data to a text file in the following way:

... //data

I want to monitor the file and get notified when certain lines are written (e.g. Start/End).

A quick google search yielded the FileSystemWatcher class but I don't think it fits since it only monitors file system events and not file content, but I could be wrong as I'm fairly new to C#/.NET.

Thanks for any help.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Something like this..  Then when the changed event fires look in the file for the changes.

You will probably need to keep counter for start and one for end then when the file changes read the file and compare the counts.

static void Main(string[] args)
            FileSystemWatcher watch = new FileSystemWatcher();
            watch.Path = @"D:\tmp";

            watch.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

            // Only watch text files.
            watch.Filter = "*.txt";

            watch.Changed += new FileSystemEventHandler(OnChanged);
            watch.Created += new FileSystemEventHandler(OnChanged);
            watch.Deleted += new FileSystemEventHandler(OnChanged);
            watch.Renamed += new RenamedEventHandler(OnRenamed);

            watch.EnableRaisingEvents = true;


        private static void OnChanged(object source, FileSystemEventArgs e)
            // Specify what is done when a file is changed, created, or deleted.
            if(e.FullPath == @"D:\tmp\p.txt")
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);


        private static void OnRenamed(object source, RenamedEventArgs e)
            // Specify what is done when a file is renamed.
            if (e.FullPath == @"D:\tmp\p.txt")
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ajnyAuthor Commented:
Hi Colemss,

Thanks for your quick reply!

I was hoping there's a better solution.

As I wrote, I'm already aware of the FileSystemWatcher class and its usages.
What I'm trying to write is a generic file watcher that can detect an arbitrary pattern (regex) being written to a file.

Using FileSystemWatcher will force me to re-read the file every time or (more likely) to keep track of where I stopped the last time and seek to that location before the next search.
Although these approaches will work, I was hoping there's a way to hook the monitor on the write operation (in a sense like a filter), so it can "see" what's about to be written to the file.
The only way I can imagine doing this would be with some sort of system hook.  It would require win32 dll hooks.  Most likely you will find c++ examples.  These would be undocumented Microsoft core hooks.  Unless this is  a major deal its probably not worth doing.  I have never seen any examples of looking at the data before or during a file write. It would be interesting.
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

You can create a  unix tail function equivalent  in c# there is already one done.
By a  FileSystemWatcher object you may check your file and determine any changes, check whether it contains Start/End blocks by Tailer class.

If you interested, check this link.
That is the full source to my first suggestion.  I would try it. But it is using FileSystemWatcher.
He has to use a timer or a filesystemwatcher to monitor changes. I believe a filesystemwatcher will have a better performance.
ajnyAuthor Commented:
Thanks for your help, guys.

I'll wait a little while to see if someone can come up with a better solution before I resort to FileSystemWatcher.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.